honkmaster Posted July 1, 2012 Share Posted July 1, 2012 Hi looking for a bit of help, I'm trying to create an upload script that send file to a directory named in the form "uploads/images/$company" I'm not bothered if the dir already exists The issue I'm have is the uploaded files don't end up in the directory set by "$company" they end up in the "images level Any help or advice would be great, its driving me made Cheers Chris <?php //testing postecho "$_POST[nouploads]";echo "<br />";echo "$_POST[company]";//posted from form$nouploads = "$_POST[nouploads]";$company = "$_POST[company]";//make a dir with the same name as company//does not matter if it already existsif (!file_exists ( "uploads/image/$company" )) {mkdir("uploads/image/$company", 0777, true);}error_reporting(E_ALL); //directory to upload to $upload_dir= "uploads/image/$company"; //numver of files to upload passed by form $num_uploads = "$nouploads"; //maximum filesize allowed in bytes $max_file_size = 2000000; //the maximum filesize from php.ini $ini_max = str_replace('M', '', ini_get('upload_max_filesize')); $upload_max = $ini_max * 2000000; //a message for users $msg = 'Please select files for uploading'; //an array to hold messages $messages = array(); //check if a file has been submitted if(isset($_FILES['userfile']['tmp_name'])) { //loop through the array of files for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++) { //check if there is a file in the array if(!is_uploaded_file($_FILES['userfile']['tmp_name'][$i])) { $messages[] = 'No file uploaded'; } //check if the file is less then the max php.ini size elseif($_FILES['userfile']['size'][$i] > $upload_max) { $messages[] = "File size exceeds $upload_max php.ini limit"; } //check the file is less than the maximum file size elseif($_FILES['userfile']['size'][$i] > $max_file_size) { $messages[] = "File size exceeds $max_file_size limit"; } else { //copy the file to the specified dir if(@copy($_FILES['userfile']['tmp_name'][$i],$upload_dir.'/'.$_FILES['userfile']['name'][$i])) { //give praise and thanks to the php gods $messages[] = $_FILES['userfile']['name'][$i].' uploaded'; } else { //an error message $messages[] = 'Uploading '.$_FILES['userfile']['name'][$i].' Failed'; } } } }?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>Multiple File Upload</title></head><body><h3><?php echo $msg; ?></h3><p><?php if(sizeof($messages) != 0) { foreach($messages as $err) { echo $err.'<br />'; } }?></p><form enctype="multipart/form-data" action="upload3.php" method="post"><input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" /><?php $num = 0; while($num < $num_uploads) { echo '<div><input name="userfile[]" type="file" /></div>'; $num++; }?><input type="submit" value="Upload" /></form></body></html> Link to comment Share on other sites More sharing options...
astralaaron Posted July 1, 2012 Share Posted July 1, 2012 I don't see the problem , did you try and echo out the file path to make sure it is correct right before you try to copy the file into the folder? Link to comment Share on other sites More sharing options...
honkmaster Posted July 2, 2012 Author Share Posted July 2, 2012 If I replace the $company bit with a company name i.e "uploads/image/$company" to "uploads/image/companyname" it works?? I just don't under stand why the files don't end up in the right place. I have echoed back and its seems to show the correct path until I submit where the company name is missing Thanks for responding Cheers Chris Link to comment Share on other sites More sharing options...
honkmaster Posted July 2, 2012 Author Share Posted July 2, 2012 Ok, I have put on error reporting and fixed errors, I have also added echo to see what is happing. All works apart from the file does not end up in the "company" directory it ends up in the "image" directory with the "company" folders. This is what is echoing back to start with "uploads/image/CompanyName/" Once the Directory is created it should put the uploaded files in the "CompanyName" directory. What i get echoed back is "uploads/image//" the "CompanyName" is missing Very confused, any help would be great Cheers Chris <?phperror_reporting(-1);//posted from formif(isset($_POST['company']))$target = "$_POST[company]";if(isset($_POST['nouploads']))$nouploads = "$_POST[nouploads]";{global $target, $nouploads, $dir;}echo $target;echo "<br />";//make a dir with the same name as company//does not matter if it already existsif (!file_exists ( "uploads/image/$target/" )) {mkdir("uploads/image/$target/", 0777, true);} //directory to upload to $upload_dir = "uploads/image/$target/";echo $upload_dir;echo "<br />"; //number of files to upload passed by form $num_uploads = "$nouploads"; //maximum filesize allowed in bytes $max_file_size = 2000000; //the maximum filesize from php.ini $ini_max = str_replace('M', '', ini_get('upload_max_filesize')); $upload_max = $ini_max * 2000000; //a message for users $msg = 'Please select files for uploading'; //an array to hold messages $messages = array(); //check if a file has been submitted if(isset($_FILES['userfile']['tmp_name'])) { //loop through the array of files for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++) { //check if there is a file in the array if(!is_uploaded_file($_FILES['userfile']['tmp_name'][$i])) { $messages[] = 'No file uploaded'; } //check if the file is less then the max php.ini size elseif($_FILES['userfile']['size'][$i] > $upload_max) { $messages[] = "File size exceeds $upload_max php.ini limit"; } //check the file is less than the maximum file size elseif($_FILES['userfile']['size'][$i] > $max_file_size) { $messages[] = "File size exceeds $max_file_size limit"; } else { //copy the file to the specified dir if(@copy($_FILES['userfile']['tmp_name'][$i],$upload_dir.'/'.$_FILES['userfile']['name'][$i])) { //give praise and thanks to the php gods $messages[] = $_FILES['userfile']['name'][$i].' uploaded'; } else { //an error message $messages[] = 'Uploading '.$_FILES['userfile']['name'][$i].' Failed'; } } } }?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>Multiple File Upload</title></head><body><h3><?php echo $msg; ?></h3><p><?php if(sizeof($messages) != 0) { foreach($messages as $err) { echo $err.'<br />'; } }?></p><form enctype="multipart/form-data" action="upload3.php" method="post"><input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" /><?php $num = 0; while($num < $num_uploads) { echo '<div><input name="userfile[]" type="file" /></div>'; $num++; }?><input type="submit" value="Upload" /></form></body></html> Link to comment Share on other sites More sharing options...
justsomeguy Posted July 2, 2012 Share Posted July 2, 2012 The form doesn't have a field called "company". Link to comment Share on other sites More sharing options...
honkmaster Posted July 2, 2012 Author Share Posted July 2, 2012 Hi I use this form to pass the number of upload fields and company name <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Untitled Document</title></head><body><form action="upload3.php" method="post">Number of Images to Upload<br />Company<input name="company" type="text" /><br /><select name="nouploads"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option></select><input type="submit" name="submit" id="submit" value="Submit" /></form></body></html> Link to comment Share on other sites More sharing options...
justsomeguy Posted July 2, 2012 Share Posted July 2, 2012 That's fine, but that's not the form that you submit to upload files, so "company" is not going to be in the $_POST array for the upload form. Either store that value in the session, or use a hidden input in the upload form to pass the value to the next page. Link to comment Share on other sites More sharing options...
honkmaster Posted July 3, 2012 Author Share Posted July 3, 2012 Thank you so much, it worked, I now understand where I was going wrong Cheers Chris Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now