PDA

View Full Version : Upload Script Using PHP


Wino
03-08-2008, 01:03 AM
I've got two domains, and I'm going to be using one to upload, store, and serve data to the other.

I've written the Flash Code to get the browse file, and I've searched the help files, here, and flash-db site to do the rest. I guess I'm just php-ignorant. Anyway, here's the basic flash code, almost directly from the Flash help files:

var fileLoc:FileReference = new FileReference();
var imageTypes:FileFilter = new FileFilter
("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png");
var textTypes:FileFilter = new FileFilter("Text Files (*.txt, *.rtf)", "*.txt; *.rtf");
var vidTypes:FileFilter = new FileFilter("Video Files (*.flv)","*.flv");
var allTypes:Array = new Array(imageTypes, textTypes, vidTypes);
fileLoc.addEventListener(Event.SELECT, selectHandler);
fileLoc.addEventListener(Event.COMPLETE, completeHandler);
try {
var success:Boolean = fileLoc.browse(allTypes);
}
catch (error:Error) {
trace("Unable to browse for files.");
}
function selectHandler(event:Event):void {
var myRequest:URLRequest =
new URLRequest("http://[domain]/fileUpload.php")
try {
fileLoc.upload(myRequest);
}
catch (error:Error) {
trace("Unable to upload file.");
}
}
function completeHandler(event:Event):void {
trace("uploaded");
}


That all works, I think (It appears to, until I select the file, anyway). I'm trying to write the fileUpload.php file now, and have found the following code, from one of the three areas I mentioned above (who knows which one?)


<?php
$MAXIMUM_FILESIZE = 1024 * 5000; // 5MB
$MAXIMUM_FILE_COUNT = 100; // keep maximum 10 files on server
echo exif_imagetype($_FILES['Filedata']);
if ($_FILES['Filedata']['size'] <= $MAXIMUM_FILESIZE)
{
move_uploaded_file($_FILES['Filedata']['tmp_name'], "./temporary/".$_FILES['Filedata']['name']);
$type = exif_imagetype("./temporary/".$_FILES['Filedata']['name']);
if ($type == 1 || $type == 2 || $type == 3)
{
rename("./temporary/".$_FILES['Filedata']['name'], "./images/".$_FILES['Filedata']['name']);
}
else
{
unlink("./temporary/".$_FILES['Filedata']['name']);
}
}
$directory = opendir('./images/');
$files = array();
while ($file = readdir($directory)) {
array_push($files, array('./images/'.$file, filectime('./images/'.$file)));
}
usort($files, sorter);
if (count($files) > $MAXIMUM_FILE_COUNT) {
$files_to_delete = array_splice($files, 0, count($files) - $MAXIMUM_FILE_COUNT);
for ($i = 0; $i < count($files_to_delete); $i++) {
unlink($files_to_delete[$i][0]);
}
}
print_r($files);
closedir($directory);

function sorter($a, $b) {
if ($a[1] == $b[1]){
return 0;
}
else {
return ($a[1] < $b[1]) ? -1 : 1;
}
}
?>


The problem is that I'm not sure how to continue. Do I upload the php file to my storage server? I'm not worried about security or sandbox or access issues, just how to proceed.

Do I have to rename some variables? How does the PHP file know what's coming, and how do they correspond? Is fileData really fileLoc from my AS file?

I will have complete access to the folders, so all directories will exist. I know some of the PHP can be eliminated, but I'm afraid to touch it.

Any and all help is appreciated. I'm not stupid. All I normally need is a cursory explanation, and a point in the right direction.

Wino
03-10-2008, 02:38 PM
I thought my questions were easier than the lack of response has shown.

Instead, can someone point me to a "quick and dirty" php explanation or tutorial so I can figure out the script myself? Even a good php book for the php beginner, not programmer beginner... if you know what I mean. (I don't need someone to explain "iteration" or "pointer," but I do need someone to tell me the vocabulary, syntax, and meaning of the PHP language, like the Flash help usually does for AS.)

ronnedith
04-20-2009, 02:29 AM
You probably resolved this, and it would be great if you could give me a hand accomplishing the same project.
Are the scripts above AS2 or AS3?
How will I make it work, so it will use an existing PHP file and send the files to the e-mail specified?
I don't want to upload the file anywhere, I just want to browse it, find it, and send it to the e-mail address.

Thank you!!

tadster
04-21-2009, 04:20 PM
hmmm... do you have permission to create files on the server? Is php running in safe mode? If not try something simple like fopen/fwrite to see if you have permission. Why are you using two domains? That will give you problems with the flash.
Have you been to php.net?

ronnedith its AS3