ThePirate Posted August 3, 2007 Share Posted August 3, 2007 Hi, I'm having a few problems with a file upload script. It doesnt seem to handle large files very well. I know the script could be tidied up but it's my first attempt at PHP and I follow the "Make it work, then fix it" philosophy. Any help will be very useful, thanks in advance. The script is hosted on my own server on my PC for now, I don't know if that would cause any problems. Here is my HTML form: <form enctype="multipart/form-data" action="upload.php" method="post"><input name="MAX_FILE_SIZE" value="10000000000000" type="hidden"> <p style="font-family: Arial;" class="MsoNormal"><small><span class="maintxt"> I agree with </span></small><span class="maintxt"></span><small><a href="aup.html">"Acceptable Use Policy"</a> </small><small><span class="maintxt"><input tabindex="1" name="tac" value="1" type="checkbox"></span></small></p> <p style="font-family: Arial;" class="MsoNormal"><small> New name: <input size="40" name="FileName"></small><small><span class="maintxt"></span></small></p> <small><span style="font-family: Arial;"> Choose a file to upload: </span></small><input name="uploadedfile" type="file"><input value="Upload" type="submit"></form> And here is my PHP script: <?php // Where the file is going to be placed $target_path = "uploads/"; /* Add the original filename to our target path. Result is "uploads/filename.extension" */ $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); $root_directory = "http://Shark/Ascension"; $file_type = $_FILES['uploadedfile']['type']; $file_name = basename( $_FILES['uploadedfile']['name']); function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } // Write data access to log file function writetolog ($message, $orig_file_name, $new_file_name) { $log_file = "logs/useruploads.log"; $ip = $_SERVER['REMOTE_ADDR']; $date_array = getdate(); $time = time(); $fp = fopen($log_file, "a"); flock($fp, LOCK_EX); fputs($fp, "---------------------------------------------------------------------- \r\n"); fputs($fp, "Status: $message \r\n" ); fputs($fp, "Date\Time: " ); fputs($fp, date("jS \o\f F Y, \a\\t g:ia \i\\n e"."\r\n", $time ) ); fputs($fp, "IP Address: $ip\r\n" ); fputs($fp, "Original File Name: $orig_file_name\r\n"); fputs($fp, "New File Name: $new_file_name\r\n"); flock($fp, LOCK_UN); fclose($fp); } // Check the CheckBox $ticked = $_POST["tac"]; // Check the TextBox $user_submitted_name = $_POST["FileName"]; // If true the user has agreed with the TAC if($ticked == '1') { // If true the user has not selected a file to upload if ($target_path == "uploads/") { print "<small><span style='font-family: Arial;'><bold style='font-weight: bold;'><br>Upload Failed</span></small></bold></a><br>"; print "<small><span style='font-family: Arial;'><br>Please specify a file you wish to upload by using the Browse button.</span></small></a>"; print "<small><span style='font-family: Arial;'><br><br><a href='http://Shark/Ascension/oneclickhosting.html'>Back</a><br></span></small>"; writetolog ("No File Selected", $file_name, "N/A" ); } else { // If true the file already exists on the server if (file_exists($target_path)) { print "<small><span style='font-family: Arial;'><bold style='font-weight: bold;'><br>Upload Failed</span></small></bold></a><br>"; print "<small><span style='font-family: Arial;'><br>There is already a file on the server that is called that. Please rename your file and upload it again. </span></small></a>"; print "<small><span style='font-family: Arial;'><br><br><a href='http://Shark/Ascension/oneclickhosting.html'>Back</a><br></span></small>"; writetolog ("File Already Exists", $file_name, "N/A" ); } else { // If true the user has tried to upload a forbidden file type if(eregi('\.php$',$file_name)) { print "<small><span style='font-family: Arial;'><bold style='font-weight: bold;'><br>Upload Failed</span></small></bold></a><br>"; print "<small><span style='font-family: Arial;'><br>You are not allowed to upload files of that type to the server. </span></small></a>"; print "<small><span style='font-family: Arial;'><br><br><a href='http://Shark/Ascension/oneclickhosting.html'>Back</a><br></span></small>"; writetolog ("Forbidden File", $file_name, "N/A" ); } else { $file_exts = findexts ($file_name); $ran = rand () ; $ran2 = $ran."."; $user_submitted_name2 = $user_submitted_name."."; if($user_submitted_name == "") { $target_path = "uploads/" . $ran2.$file_exts; $new_file_name = $ran2.$file_exts; } else { $target_path = "uploads/" . $user_submitted_name2.$file_exts; $new_file_name = $user_submitted_name2.$file_exts; } // If true the file already exists on the server if (file_exists($new_file_name) || file_exists($file_name)) { print "<small><span style='font-family: Arial;'><bold style='font-weight: bold;'><br>Upload Failed</span></small></bold></a><br>"; print "<small><span style='font-family: Arial;'><br>There is already a file on the server that is called that. Please rename your file and upload it again. </span></small></a>"; print "<small><span style='font-family: Arial;'><br><br><a href='http://Shark/Ascension/oneclickhosting.html'>Back</a><br></span></small>"; writetolog ("File Already Exists", $file_name, $new_file_name ); } // If true the upload has succeeded if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { print "<small><span style='font-family: Arial;'><bold style='font-weight: bold;'><br>Upload Succeeded</span></small></bold></a><br>"; print "<small><span style='font-family: Arial;'><br> The file <bold style='font-weight: bold;'>'$file_name'</bold> has been succesfully uploaded to the server and renamed to <bold style='font-weight: bold;'>'$new_file_name'</bold>, if you wish to access it at any time please go to the following link - <a href='$root_directory/$target_path'>$root_directory/$target_path</span></small></a>"; print "<small><span style='font-family: Arial;'><br><br>To remove this file please go to the following link - <a href='$root_directory/filemanage.php?file_name=$new_file_name'>$root_directory/filemanage.php?file_name=$new_file_name</span></small></a>"; print "<small><span style='font-family: Arial;'><br><br><a href='http://Shark/Ascension/oneclickhosting.html'>Back</a><br></span></small>"; writetolog ("File Upload Succeed", $file_name, $new_file_name ); } // The upload has failed else { print "<small><span style='font-family: Arial;'><bold style='font-weight: bold;'><br>Upload Failed</span></small></bold></a><br>"; print "<small><span style='font-family: Arial;'><br> There was an error uploading the file to the server, please try again!</span></small>"; print "<small><span style='font-family: Arial;'><br><br><a href='http://Shark/Ascension/oneclickhosting.html'>Back</a><br></span></small>"; writetolog ("File Upload Failed", $file_name, $new_file_name ); } } } } } // The user has not ticked the check box else { header("location: oneclickhosting_tac_error.html"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/63192-php-file-upload-help/ 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.