Hi there, I got some small questions about some fileupload functions:
If I use the function isset on $FILES['file']['name'] it always returs true... even if there was no value in that field, so I have to use the function $FILES['file']['name'] == ""... why is that?
How does the $_FILES['file']['type'] work? I'm writing a picture upload script, and I need to be REALY sure that it's a picture that is uploaded and not any other type of file... I tried to create a text file and renamed it to picture.jpg - and the server rejected the file. Is that funtion safe to use for validating or is that just a coincidence?
Thanks!
Firstly to check if the file is uploaded
use $_FILES['file']['error'] if == 0 then its uploaded.
Secondly $_FILES['file']['type']
Works like so
image/extention
So JPEG would be image/pjpeg and gif would be image/gif
Upload Files! 1. Make Folder Named uploads 2. Paste On A Paste And Name It uploader.php 3. Go To uploader.php To Upload Files
<? $Admin[UploadNum] = "10"; // Number of upload fields to put on the html page $Admin[Directory] = "uploads"; if($FileUpload) { for ($Number = 1; $Number <= $Admin[UploadNum]; $Number++){ $PictureFirst = "fileup$Number"."_name";//Set format for variable $OriginalFileName $OriginalFileName = $$PictureFirst;//Somehow outputs the original file name. $SetFormatTempFile = "fileup$Number";//Set format for variable $TemporaryFile $TemporaryFile = $$SetFormatTempFile; if($TemporaryFile != "none") { $Filesizebtyes = filesize($TemporaryFile); $ok = 1; if($Filesizebtyes < 10) { $error .= "Error uploading (file size lower than 10 bytes) for file $Number<BR>"; $ok = 2; } if(file_exists("$Admin[Directory]/$OriginalFileName") OR $ok == 2) { $error .="File name already exists for file $Number<BR>"; } else { copy ($TemporaryFile, "$Admin[Directory]/$OriginalFileName"); $error .="File $Number has been uploaded<BR>"; } } } if(!$error) { $error .= "No files have been selected for upload"; } echo $error; exit(); } else { echo "<form enctype=\"multipart/form-data\" action=\"uploader.php\" method=\"post\">\n"; for ($Number = 1; $Number <= $Admin[UploadNum]; $Number++){ echo "<input name=\"fileup$Number\" type=\"file\" size=\"25\">\n"; } echo "<input name=\"FileUpload\" type=\"submit\" value=\"Upload Files\">\n"; exit(); } ?>