son.of.the.morning Posted September 7, 2010 Share Posted September 7, 2010 I would like to have more of a understanding of this upload file script if any one would like to be a pal and explain whats going on step by step, thats if it's not to much to ask of course . <?php /* checking the file extension*/ if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png"))) { /* this code will be executed if the file extension matches they above (gif,jpeg,png)*\ if ($_FILES["file"]["error"] > 0) /*if more than 0 errors occur the following code will be executed*/ { /* return code (string), $_FILES...(file var)*/ echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { /*listing file details, name, file type, file size*/ echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; /*temp file location*/ echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"])) { /* if there is a file already existing with the same name as the current file in a procedure then the following code will be executed*/ echo $_FILES["file"]["name"] . " already exists. "; } else { /*moving the successfully uploaded file to the a designated location, in this case upload/.*/ move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { /*this section refers from the first if statement, if the file extension didn’t match the files listed at the beginning of this script then the following code will be executed.*/ echo "Invalid file extention” } ?> Quote Link to comment Share on other sites More sharing options...
objnoob Posted September 7, 2010 Share Posted September 7, 2010 It's already commented for you Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted September 7, 2010 Author Share Posted September 7, 2010 haha i know i commented it but i still feel thre is more to it thats am not geting. Quote Link to comment Share on other sites More sharing options...
objnoob Posted September 7, 2010 Share Posted September 7, 2010 Which part would you like a more understanding of? Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted September 7, 2010 Author Share Posted September 7, 2010 i dont get this "error" does it mean an error during the uload echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; /1024 ??? echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; other than these i think i get it Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 7, 2010 Share Posted September 7, 2010 i dont get this "error" does it mean an error during the uload echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; $_FILES["file"]["error"] will contain an error code if their is a problem when uploading the file. All possible error codes are stated in the manual here http://php.net/manual/en/features.file-upload.errors.php /1024 ??? echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; $_FILES["file"]["size"] stores the files size of the uploaded file. The file size is measured in bytes. This operation here $_FILES["file"]["size"] / 1024 Is dividing (thats what the / stands for) the uploaded files file size by 1024 to convert the file size from bytes to kilobytes (There are 1024 bytes in a kilobyte (KB)) For more information on how PHP handles file uploads have read of the documentation here http://php.net/manual/en/features.file-upload.php Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted September 7, 2010 Author Share Posted September 7, 2010 Thanx once again dude! Quote Link to comment Share on other sites More sharing options...
ignace Posted September 8, 2010 Share Posted September 8, 2010 If you have trouble understanding your code or someone else's consider re-factoring the code: if(upload_exists($_FILES['file'], 'directory')) { echo 'Upload already exists.'; } else if(!upload_is_valid($_FILES['file'])) { echo 'Invalid upload.'; } else if(!upload_store($_FILES['file'])) { echo 'Could not store the file.'; } else { echo 'File successfully created.'; } function upload_is_valid($upload, $valid_mime, $valid_ext) { if($upload['error'] != UPLOAD_ERR_OK) return false; $finfo = finfo_open(FILEINFO_MIME_TYPE); // Note: PHP 5.3.x if(!in_array(finfo_file($finfo, $upload['tmp_name']), (array) $valid_mime)) return false; if(!in_array(pathinfo($upload['tmp_name'], PATHINFO_EXTENSION), (array) $valid_ext)) return false; return true; } function upload_exists($upload, $directory) { $filename = pathinfo($upload['tmp_name'], PATHINFO_FILENAME); return file_exists(rtrim($directory, '\/') . DIRECTORY_SEPARATOR . $filename); } function upload_store($upload, $directory) { $upload = upload_create($upload); $filename = pathinfo($upload['tmp_name'], PATHINFO_FILENAME); return move_uploaded_file(rtrim($directory, '\/') . DIRECTORY_SEPARATOR . $filename); } function upload_create($upload) { // re-create the image return $upload; } Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted September 8, 2010 Author Share Posted September 8, 2010 i understand yours allot less then i do mine. lol Quote Link to comment Share on other sites More sharing options...
ignace Posted September 8, 2010 Share Posted September 8, 2010 So these 9 lines are harder to comprehend then your 30-40 lines of code? if(upload_exists($_FILES['file'], 'directory')) { echo 'Upload already exists.'; } else if(!upload_is_valid($_FILES['file'])) { echo 'Invalid upload.'; } else if(!upload_store($_FILES['file'])) { echo 'Could not store the file.'; } else { echo 'File successfully created.'; } Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted September 8, 2010 Author Share Posted September 8, 2010 I was actualy refering to... function upload_is_valid($upload, $valid_mime, $valid_ext) { if($upload['error'] != UPLOAD_ERR_OK) return false; $finfo = finfo_open(FILEINFO_MIME_TYPE); // Note: PHP 5.3.x if(!in_array(finfo_file($finfo, $upload['tmp_name']), (array) $valid_mime)) return false; if(!in_array(pathinfo($upload['tmp_name'], PATHINFO_EXTENSION), (array) $valid_ext)) return false; return true;}function upload_exists($upload, $directory) { $filename = pathinfo($upload['tmp_name'], PATHINFO_FILENAME); return file_exists(rtrim($directory, '\/') . DIRECTORY_SEPARATOR . $filename);}function upload_store($upload, $directory) { $upload = upload_create($upload); $filename = pathinfo($upload['tmp_name'], PATHINFO_FILENAME); return move_uploaded_file(rtrim($directory, '\/') . DIRECTORY_SEPARATOR . $filename);}function upload_create($upload) { // re-create the image return $upload;} Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted September 8, 2010 Author Share Posted September 8, 2010 This part even...[ function upload_is_valid($upload, $valid_mime, $valid_ext) { if($upload['error'] != UPLOAD_ERR_OK) return false; $finfo = finfo_open(FILEINFO_MIME_TYPE); // Note: PHP 5.3.x if(!in_array(finfo_file($finfo, $upload['tmp_name']), (array) $valid_mime)) return false; if(!in_array(pathinfo($upload['tmp_name'], PATHINFO_EXTENSION), (array) $valid_ext)) return false; return true;}function upload_exists($upload, $directory) { $filename = pathinfo($upload['tmp_name'], PATHINFO_FILENAME); return file_exists(rtrim($directory, '\/') . DIRECTORY_SEPARATOR . $filename);}function upload_store($upload, $directory) { $upload = upload_create($upload); $filename = pathinfo($upload['tmp_name'], PATHINFO_FILENAME); return move_uploaded_file(rtrim($directory, '\/') . DIRECTORY_SEPARATOR . $filename);}function upload_create($upload) { // re-create the image return $upload; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.