alil2cul4u Posted December 1, 2009 Share Posted December 1, 2009 Hey, I would like some help on creating an upload script! I would appreciate it if someone could just give me a copy of their upload script.... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/183502-php-upload-script/ Share on other sites More sharing options...
mikesta707 Posted December 1, 2009 Share Posted December 1, 2009 Give you a copy, No. This is a help board, not a get free scripts board. Here is a tutorial tho: http://www.tizag.com/phpT/fileupload.php Quote Link to comment https://forums.phpfreaks.com/topic/183502-php-upload-script/#findComment-968598 Share on other sites More sharing options...
Jnerocorp Posted December 1, 2009 Share Posted December 1, 2009 what are u trying to upload any fiels or jsut specific files Quote Link to comment https://forums.phpfreaks.com/topic/183502-php-upload-script/#findComment-968613 Share on other sites More sharing options...
alil2cul4u Posted December 1, 2009 Author Share Posted December 1, 2009 Im trying to upload any file.... Like all files without restrictions.... Quote Link to comment https://forums.phpfreaks.com/topic/183502-php-upload-script/#findComment-968719 Share on other sites More sharing options...
alil2cul4u Posted December 1, 2009 Author Share Posted December 1, 2009 For some reason that tutorial is not working..... I just keep getting an error Quote Link to comment https://forums.phpfreaks.com/topic/183502-php-upload-script/#findComment-968722 Share on other sites More sharing options...
mikesta707 Posted December 1, 2009 Share Posted December 1, 2009 try posting the code you have Quote Link to comment https://forums.phpfreaks.com/topic/183502-php-upload-script/#findComment-968723 Share on other sites More sharing options...
alil2cul4u Posted December 1, 2009 Author Share Posted December 1, 2009 I have 2 the .html and the .php Upload.htm <form enctype="multipart/form-data" action="uploader.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> Choose a file to upload: <input name="uploadedfile" type="file" /><br /> <input type="submit" value="Upload File" /> </form> Uploader.php <?php $target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/183502-php-upload-script/#findComment-968728 Share on other sites More sharing options...
mikesta707 Posted December 1, 2009 Share Posted December 1, 2009 What error are you getting? Quote Link to comment https://forums.phpfreaks.com/topic/183502-php-upload-script/#findComment-968736 Share on other sites More sharing options...
alil2cul4u Posted December 1, 2009 Author Share Posted December 1, 2009 There was an error uploading the file, please try again! I cannot for the life of me figure out what is wrong with it.... I tried uploading a song that is in .m4p format and is 4.54 mb Quote Link to comment https://forums.phpfreaks.com/topic/183502-php-upload-script/#findComment-968740 Share on other sites More sharing options...
PFMaBiSmAd Posted December 1, 2009 Share Posted December 1, 2009 The form processing code in that tutorial is so minimal that it has almost no error checking, error reporting, or error recovery logic to get it to tell you why it is failing. Edit: And even the following code is not checking everything possible. Give this a try (you will need to modify the size settings and permitted mime types to suit your needs) - <?php // upload processing code with full upload-error testing logic ini_set("display_errors", "1"); error_reporting(E_ALL); // check if the page reqeust came from an upload form (to the best of php's ability) and if php can process uploads function check_request(){ // check if a form submitted to this code (exit/die if not) if(strtolower($_SERVER['REQUEST_METHOD']) !== 'post'){ echo "Page was not accessed through a post method form<br />"; return FALSE; // this code was reached via a request that was not a form submission } // check if uploads are enabled - // ini_get(), on gives a string with '1' (off is empty string or a string with '0') if(ini_get('file_uploads') !== '1'){ echo "Uploads are not enabled on this server and nothing can be uploaded<br />"; return FALSE; // cannot continue } // check for exceeding the post_max_size value (both the $_POST and $_FILES arrays are empty) or the form does not contan any named $_POST variable or it does not contain a file upload field $post_max = ini_get('post_max_size'); if(empty($_FILES) && empty($_POST)){ echo "Either the upload exceeded the post_max_size setting: $post_max, or the form is invalid (no enctype and/or no named file/post fields)<br />"; return FALSE; // cannot continue } // check for a form that contains a $_POST variable, but does not contain an upload enctype or it does not contain a file upload field if(empty($_FILES) && !empty($_POST)){ echo "Either the form does not contain a correct enctype attribute or it does not contain a valid named file upload field<br />"; return FALSE; // cannot continue } // $_FILES contains something return TRUE; } // end of check_request function // check the passed $_FILES... array for upload errors, mime type, and file size function check_upload($file){ // test for upload errors if($file['error'] > 0){ echo "An upload error occured, error number: {$file['error']}<br />"; switch ($file['error']) { case 0: echo "The file uploaded to the server OK<br />"; break; case 1: echo "The uploaded file exceeds the upload maximum size directive in php.ini<br />"; break; case 2: echo "The uploaded file exceeds {$_POST['MAX_FILE_SIZE']}, the MAX_FILE_SIZE directive in the HTML form<br />"; break; case 3: echo "The uploaded file was only partially uploaded<br />"; break; case 4: echo "No file was selected<br />"; break; case 6: echo "Missing a temporary folder<br />"; break; case 7: echo "Failed to write file to disk<br />"; break; case 8: echo "File upload stopped by extension<br />"; break; default: echo "An unused error value was returned<br />"; } return FALSE; // cannot continue } // check the mime type $types = array(); $types[] = 'image/gif'; $types[] = 'image/jpeg'; $types[] = 'image/pjpeg'; if(!in_array($file['type'],$types)){ // not found echo "The mime type of the uploaded file: {$file['type']}, is not permitted<br />"; return FALSE; // cannot continue } // check the file size $max_size = 200000000; if($file['size'] > $max_size){ // exceeds max echo "The file size of the uploaded file: {$file['size']}, exceeds the permitted file size of $max_size<br />"; return FALSE; // cannot continue } return TRUE; } // end of check_upload function // main code $target_path = "uploads/"; $files_index = "uploadedfile"; // check if the request for this page can be processed (came from a form and uploads are enabled on the server...) if(!check_request()){ // the request for the page cannot be processed die; } // the $_FILES array has something in it, $_POST array may or may not depending on the actual form fields (should at least have a named submit button) // start normal form processing code if(isset($_POST['submit'])){ // check the uploaded file for errors, mime type, and file size if(check_upload($_FILES[$files_index])){ // the uploaded file can be moved // test if the destination file already exists if(file_exists($target_path . $_FILES[$files_index]["name"])){ echo "{$_FILES[$files_index]['name']} already exists.<br />"; } else { // move the uplaoded file if(!move_uploaded_file($_FILES[$files_index]["tmp_name"], $target_path . $_FILES[$files_index]["name"])){ // the move failed echo "Could not process (move) the uploaded file<br />"; } else { echo "Uploaded file was stored in: $target_path{$_FILES[$files_index]["name"]}<br />"; // other code to process the uploaded file would go here... } } } else { // echo "check_upload() returned FALSE and should have printed an error message why the upload failed<br />"; } } // end of form processing code echo "done"; ?> You will also need to give the submit button a name - <form enctype="multipart/form-data" action="uploader.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> Choose a file to upload: <input name="uploadedfile" type="file" /><br /> <input type="submit" name="submit" value="Upload File" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/183502-php-upload-script/#findComment-968742 Share on other sites More sharing options...
alil2cul4u Posted December 1, 2009 Author Share Posted December 1, 2009 What are mime types? Quote Link to comment https://forums.phpfreaks.com/topic/183502-php-upload-script/#findComment-968743 Share on other sites More sharing options...
PFMaBiSmAd Posted December 1, 2009 Share Posted December 1, 2009 http://en.wikipedia.org/wiki/MIME_type Quote Link to comment https://forums.phpfreaks.com/topic/183502-php-upload-script/#findComment-968745 Share on other sites More sharing options...
alil2cul4u Posted December 1, 2009 Author Share Posted December 1, 2009 Ok, I see what that is.... Um, Im a noob at PHP and I dont see where this code is set to save the upload files? Quote Link to comment https://forums.phpfreaks.com/topic/183502-php-upload-script/#findComment-968746 Share on other sites More sharing options...
alil2cul4u Posted December 1, 2009 Author Share Posted December 1, 2009 To test I added audio/mpeg and tried to upload a test mp3 song that is 3.36 mb and I got this error ]An upload error occured, error number: 2 The uploaded file exceeds 100000, the MAX_FILE_SIZE directive in the HTML form done Quote Link to comment https://forums.phpfreaks.com/topic/183502-php-upload-script/#findComment-968748 Share on other sites More sharing options...
alil2cul4u Posted December 1, 2009 Author Share Posted December 1, 2009 Ok, So I fixed that problem... I didnt reset the Max_file_size... Now I have this error and I do not understand this one at all Warning: move_uploaded_file(uploads/song.mp3) [function.move-uploaded-file]: failed to open stream: No such file or directory in G:\wamp\www\uploader.php on line 108 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\php2E11.tmp' to 'uploads/song.mp3' in G:\wamp\www\uploader.php on line 108 Could not process (move) the uploaded file done Quote Link to comment https://forums.phpfreaks.com/topic/183502-php-upload-script/#findComment-968751 Share on other sites More sharing options...
PFMaBiSmAd Posted December 1, 2009 Share Posted December 1, 2009 The directory uploads does not exist. Quote Link to comment https://forums.phpfreaks.com/topic/183502-php-upload-script/#findComment-968752 Share on other sites More sharing options...
alil2cul4u Posted December 1, 2009 Author Share Posted December 1, 2009 EVERYONE THAT HELPED WITH THIS THANKS SOOOO MUCH! I GOT IT TOOO WORK. Quote Link to comment https://forums.phpfreaks.com/topic/183502-php-upload-script/#findComment-968762 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.