Gamerz Posted December 31, 2009 Share Posted December 31, 2009 Whenever I upload a file or two, it won't upload. It'd just lead me to error. What is wrong? TEST IT OUT IF YOU LIKE ON YOUR OWN SERVER Here is the form: <script type="text/javascript"> var i = 1; function add_file(file) { var j = i - 1; var box = document.getElementById('file_list'); var num = box.length; var file_exists = 0; for (x = 0; x < num; x++) { if (box.options[x].text == file) { alert('This file has already been added to the list.'); document.getElementById('file_' + j).value = ""; file_exists = 1; break; } } if (file_exists == 0) { // For Internet Explorer try { el = document.createElement('<input type="file" name="userfile[]" id="file_' + i + '" onChange="javascript:add_file(this.value);">'); } // For other browsers catch (e) { el = document.createElement('input'); el.setAttribute('type', 'file'); el.setAttribute('name', 'userfile[]'); el.setAttribute('id', 'file_' + i); el.setAttribute('onChange', 'javascript:add_file(this.value);'); } document.getElementById('file_' + j).style.display = 'none'; if (document.getElementById('list_div').style.display == 'none') { document.getElementById('list_div').style.display = 'block'; } document.getElementById('files_div').appendChild(el); box.options[num] = new Option(file, 'file_' + j); i++; } } function remove_file() { var box = document.getElementById('file_list'); if (box.selectedIndex != -1) { var value = box.options[box.selectedIndex].value; var child = document.getElementById(value); box.options[box.selectedIndex] = null; document.getElementById('files_div').removeChild(child); if (box.length == 0) { document.getElementById('list_div').style.display = 'none'; } } else { alert('You must first select a file from the list.'); } } function do_submit() { // Uncomment this block for the real onSubmit code /* var box = document.getElementById('file_list'); var max_files = 5; if (box.length <= max_files) { var child = document.getElementById('file_' + (i - 1)); div = document.getElementById('files_div'); div.removeChild(child); div.style.display = 'none'; return true; } else { alert('You have more files listed than the maximum allowed.\nPlease limit your upload files to no more than <? echo $upload_max_files; ?> at a time.'); return false; } */ // Just for test page alert('Files uploaded successfully'); return false; } </script> <form enctype="multipart/form-data" action="add.php" method="post"> <div name="files_div" id="files_div"> Select a File: <input type="file" name="userfile[]" id="file_0" onChange="javascript:add_file(this.value);"> </div> <br><br> <div name="list_div" id="list_div" style="display: none;"> <select multiple name="file_list" id="file_list"></select> <br><br> <input type="button" id="remove_file_btn" value="Remove Selected" onClick="javascript:remove_file();"> <br> <br><br>Destination to Upload (?): <select name="uploadto"> <option>uploads/</option> <?php // Your start directory getDirectory('/home/40/d53451j374/htd/home/'); function getDirectory( $path = '.', $level = 0 ) { // Directories to ignore when listing output. $ignore = array( '.', '..' ); // Open the directory to the handle $dh $dh = @opendir( $path ); // Loop through the directory while( false !== ( $file = readdir( $dh ) ) ) { // Check that this file is not to be ignored if( !in_array( $file, $ignore ) ) { // Show directories only if(is_dir( "$path/$file" ) ) { // Re-call this same function but on a new directory. // this is what makes function recursive. ?> <?php $slash = "/"; ?> <?php echo "<option>$file$slash</option>"; ?> <?php getDirectory( "$path/$file", ($level+1) ); ?> <?php } } } // Close the directory handle closedir( $dh ); } ?> </select> <input type="Submit" name="upload" value="Upload"> Here is the processing script: <?php $dir_upload = "/home/40/d53451j374/htd/home/uploads/"; $uploadto = $_POST['uploadto']; $upload_path = $dir_upload.$uploadto; // Copy global $_FILES array to new $files array and strip out any empty objects $files = array(); $x = 0; foreach ($_FILES as $key => $value) { if ((!is_null($value)) && ($value !== "")) { $files[$x] = $value; $x++; } } // Count number of files uploaded $num_files = count($files[0]['tmp_name']); for ($i = 0; $i < $num_files; $i++) { // Shorten variable names $tmp_file = $files[0]['tmp_name'][$i]; $filename = $files[0]['name'][$i]; // If the upload was successful... if (is_uploaded_file($tmp_file)) { $copy = copy($dir_upload . $uploadto . $filename); echo $dir_upload . $uploadto . $filename; if($copy) { echo "<center>The file name: $filename has been uploaded sucessfully<br><br><b>The File URL for $filename is: <a href=\"nothing here yet"\></a></b><br><hr>"; // Delete temporary uploaded file unlink($tmp_file); } // ... otherwise, print an error else { echo "Error uploading file <b>$filename</b>.<br><br>"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/186758-php-what-is-wrong-with-this-file-upload/ Share on other sites More sharing options...
premiso Posted December 31, 2009 Share Posted December 31, 2009 You do have a syntax error here : echo "<center>The file name: $filename has been uploaded sucessfully<br><br><b>The File URL for $filename is: <a href=\"nothing here yet"\></a></b><br><hr>"; Change it to: echo "<center>The file name: $filename has been uploaded sucessfully<br><br><b>The File URL for $filename is: <a href=\"nothing here yet\"></a></b><br><hr>"; Not sure if that was the error you were talking about, as you never stated what error you were getting. Quote Link to comment https://forums.phpfreaks.com/topic/186758-php-what-is-wrong-with-this-file-upload/#findComment-986244 Share on other sites More sharing options...
Gamerz Posted December 31, 2009 Author Share Posted December 31, 2009 Not a php error...it was just that the file didn't actually upload. And no, that is not the error...the file doesn't upload. Can you see anything else? Quote Link to comment https://forums.phpfreaks.com/topic/186758-php-what-is-wrong-with-this-file-upload/#findComment-986247 Share on other sites More sharing options...
premiso Posted December 31, 2009 Share Posted December 31, 2009 I have never used copy for uploaded files mainly because there is a move_uploaded_file which you would not have to use unlink after. Given that I found a few different issues, the main one was you were not giving the tmp_file location to the copy. Here is a revamped script, encoupled with $_SERVER['DOCUMENT_ROOT'] so it will work on other servers then your own. add.php: <?php $dir_upload = $_SERVER['DOCUMENT_ROOT']; $uploadto = $_POST['uploadto']; $upload_path = $dir_upload.$uploadto; // Copy global $_FILES array to new $files array and strip out any empty objects $files = array(); $x = 0; foreach ($_FILES as $key => $value) { if ((!is_null($value)) && ($value !== "") ) { $files[$x] = $value; $x++; } } // Count number of files uploaded $num_files = count($files[0]['tmp_name']); for ($i = 0; $i < $num_files; $i++) { // Shorten variable names $tmp_file = $files[0]['tmp_name'][$i]; $filename = $files[0]['name'][$i]; // If the upload was successful... if (is_uploaded_file($tmp_file)) { $move = move_uploaded_file($tmp_file, $dir_upload . $uploadto . $filename); echo $dir_upload . $uploadto . $filename; if($move) { echo "<center>The file name: $filename has been uploaded sucessfully<br><br><b>The File URL for $filename is: <a href=\"nothing here yet\"></a></b><br><hr>"; } // ... otherwise, print an error else { echo "Error uploading file <b>$filename</b>.<br><br>"; } }else { echo "Error: Not an uploaded file"; } } ?> upload.php <script type="text/javascript"> var i = 1; function add_file(file) { var j = i - 1; var box = document.getElementById('file_list'); var num = box.length; var file_exists = 0; for (x = 0; x < num; x++) { if (box.options[x].text == file) { alert('This file has already been added to the list.'); document.getElementById('file_' + j).value = ""; file_exists = 1; break; } } if (file_exists == 0) { // For Internet Explorer try { el = document.createElement('<input type="file" name="userfile[]" id="file_' + i + '" onChange="javascript:add_file(this.value);">'); } // For other browsers catch (e) { el = document.createElement('input'); el.setAttribute('type', 'file'); el.setAttribute('name', 'userfile[]'); el.setAttribute('id', 'file_' + i); el.setAttribute('onChange', 'javascript:add_file(this.value);'); } document.getElementById('file_' + j).style.display = 'none'; if (document.getElementById('list_div').style.display == 'none') { document.getElementById('list_div').style.display = 'block'; } document.getElementById('files_div').appendChild(el); box.options[num] = new Option(file, 'file_' + j); i++; } } function remove_file() { var box = document.getElementById('file_list'); if (box.selectedIndex != -1) { var value = box.options[box.selectedIndex].value; var child = document.getElementById(value); box.options[box.selectedIndex] = null; document.getElementById('files_div').removeChild(child); if (box.length == 0) { document.getElementById('list_div').style.display = 'none'; } } else { alert('You must first select a file from the list.'); } } function do_submit() { // Uncomment this block for the real onSubmit code /* var box = document.getElementById('file_list'); var max_files = 5; if (box.length <= max_files) { var child = document.getElementById('file_' + (i - 1)); div = document.getElementById('files_div'); div.removeChild(child); div.style.display = 'none'; return true; } else { alert('You have more files listed than the maximum allowed.\nPlease limit your upload files to no more than <? echo $upload_max_files; ?> at a time.'); return false; } */ // Just for test page alert('Files uploaded successfully'); return false; } </script> <form enctype="multipart/form-data" action="add.php" method="post"> <div name="files_div" id="files_div"> Select a File: <input type="file" name="userfile[]" id="file_0" onChange="javascript:add_file(this.value);"> </div> <br><br> <div name="list_div" id="list_div" style="display: none;"> <select multiple name="file_list" id="file_list"></select> <br><br> <input type="button" id="remove_file_btn" value="Remove Selected" onClick="javascript:remove_file();"> <br> <br><br>Destination to Upload (?): <select name="uploadto"> <option>uploads/</option> <?php function getDirectory( $path = '.', $level = 0 ) { // Directories to ignore when listing output. $ignore = array( '.', '..' ); // Open the directory to the handle $dh $dh = @opendir( $path ); // Loop through the directory while( false !== ( $file = readdir( $dh ) ) ) { // Check that this file is not to be ignored if( !in_array( $file, $ignore ) ) { // Show directories only if(is_dir( "$path/$file" ) ) { // Re-call this same function but on a new directory. // this is what makes function recursive. $slash = "/"; echo "<option>$file$slash</option>"; getDirectory( "$path/$file", ($level+1) ); } } } // Close the directory handle closedir( $dh ); } // Your start directory getDirectory($_SERVER['DOCUMENT_ROOT'] . 'uploads/'); ?> </select> <input type="Submit" name="upload" value="Upload"> I tested it and it worked alright on my server. EDIT: Modified the count statement to be correct. Quote Link to comment https://forums.phpfreaks.com/topic/186758-php-what-is-wrong-with-this-file-upload/#findComment-986254 Share on other sites More sharing options...
premiso Posted December 31, 2009 Share Posted December 31, 2009 Sorry, here is a corrected add.php as I got confused and it was doing some weird stuff (1 upload worked but mutiple would not). This version does work, I changed how the $files array is structured / populated. Questions let me know: <?php $dir_upload = $_SERVER['DOCUMENT_ROOT']; $uploadto = $_POST['uploadto']; $upload_path = $dir_upload.$uploadto; // Copy global $_FILES array to new $files array and strip out any empty objects $files = array(); $x = 0; foreach ($_FILES['userfile']['error'] as $key => $value) { if ($value == 0 ) { $files[$x]['tmp_name'] = $_FILES['userfile']['tmp_name'][$key]; $files[$x]['name'] = $_FILES['userfile']['name'][$key]; $x++; } } // Count number of files uploade for ($i = 0; $i < $x; $i++) { // Shorten variable names $tmp_file = $files[$i]['tmp_name']; $filename = $files[$i]['name']; // If the upload was successful... if (is_uploaded_file($tmp_file)) { $move = move_uploaded_file($tmp_file, $dir_upload . $uploadto . $filename); echo $dir_upload . $uploadto . $filename; if($move) { echo "<center>The file name: $filename has been uploaded sucessfully<br><br><b>The File URL for $filename is: <a href=\"nothing here yet\"></a></b><br><hr>"; } // ... otherwise, print an error else { echo "Error uploading file <b>$filename</b>.<br><br>"; } }else { echo "Error: Not an uploaded file"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/186758-php-what-is-wrong-with-this-file-upload/#findComment-986256 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.