sandbudd Posted July 5, 2008 Share Posted July 5, 2008 This is to put a upload into a folder to be called by the db but the file is not uploading...any suggestions? /* A function for moving file uploads, please make sure the destination directory has directory permissions set (777) */ function move_upload( $file, $dest, $overwrite = false ) { if ($file["error"] == UPLOAD_ERR_OK) { if(!file_exists( $dest.$file["name"] ) || $overwrite) { if (move_uploaded_file( $file["tmp_name"], $dest.$file["name"] )) { $upload_feedback .= "The file " . $file["name"] . " was successfully uploaded"; $error = FALSE; } else { $upload_feedback .= "The file " . $file["name"] . " could not be moved"; $error = TRUE; } } else { $upload_feedback .= "The file " . $file["name"] . " already exists, please check the overwrite option if you wish to replace it"; $error = TRUE; } } else { switch ($file["error"]) { case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: $upload_feedback .= "The file " . $file["name"] . " is to large to be uploaded<br />"; $error = TRUE; break; case UPLOAD_ERR_PARTIAL: $upload_feedback .= "The file" . $file["name"] . " was interrupted while uploading, please try again<br />"; $error = TRUE; break; } } return array( "error" => $error, "feedback" => $upload_feedback ); //return message plus error status } /* All of the uploaded images */ $attachment = $_FILES["attachment"]; /* Destination directory */ $destination = "../upload/"; /* Call the function to move the files */ move_upload( $attachment, $destination ); ?> <?php mysql_query("INSERT INTO `items` (`attachment`) VALUES('{$attachment["name"]}')") or die(mysql_error()); ?> ] Quote Link to comment Share on other sites More sharing options...
ron8000 Posted July 5, 2008 Share Posted July 5, 2008 What error messages form your script are you getting? are you getting PHP errors? are you using error_reporting(E_ALL)? Quote Link to comment Share on other sites More sharing options...
sandbudd Posted July 5, 2008 Author Share Posted July 5, 2008 im not getting any error and how do I use the error reporting Quote Link to comment Share on other sites More sharing options...
ron8000 Posted July 5, 2008 Share Posted July 5, 2008 At the top of the file you have posted add a line <?php error_reporting(E_ALL); ?> This should report all errors to you. Also check the save to path to make sure it's a there and b able to have files written to it. Quote Link to comment Share on other sites More sharing options...
sandbudd Posted July 5, 2008 Author Share Posted July 5, 2008 I changed to a different server and I get this error... Notice: Undefined index: attachment in /home/anodizi/public_html/image/upload2.php on line 70 Notice: Undefined variable: _files in /home/anodizi/public_html/image/upload2.php on line 76 Notice: Undefined variable: files_ in /home/anodizi/public_html/image/upload2.php on line 76 here is the code: <?php /* A function for moving file uploads, please make sure the destination directory has directory permissions set (777) */ function move_upload( $file, $dest, $overwrite = false ) { if ($file["error"] == UPLOAD_ERR_OK) { if(!file_exists( $dest.$file["name"] ) || $overwrite) { if (move_uploaded_file( $file["tmp_name"], $dest.$file["name"] )) { $upload_feedback .= "The file " . $file["name"] . " was successfully uploaded"; $error = FALSE; } else { $upload_feedback .= "The file " . $file["name"] . " could not be moved"; $error = TRUE; } } else { $upload_feedback .= "The file " . $file["name"] . " already exists, please check the overwrite option if you wish to replace it"; $error = TRUE; } } else { switch ($file["error"]) { case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: $upload_feedback .= "The file " . $file["name"] . " is to large to be uploaded<br />"; $error = TRUE; break; case UPLOAD_ERR_PARTIAL: $upload_feedback .= "The file" . $file["name"] . " was interrupted while uploading, please try again<br />"; $error = TRUE; break; } } return array( "error" => $error, "feedback" => $upload_feedback ); //return message plus error status } /* All of the uploaded images */ $attachment = $_FILES["attachment"]; /* Call the function to move the files */ move_uploaded_file($_files['attachment']['tmp_name'],"http://fortwayneanodizing.com/image/upload/".$files_['attachment']['name']) ; ?> <?php mysql_query("INSERT INTO `upload` (`attachment`) VALUES('{$attachment["name"]}')") or die(mysql_error()); ?> Quote Link to comment Share on other sites More sharing options...
ron8000 Posted July 5, 2008 Share Posted July 5, 2008 Now i'm not able to see line numbers but from what i can ready Notice: Undefined index: attachment in /home/anodizi/public_html/image/upload2.php on line 70 I believe this is your line 70? -> $attachment = $_FILES["attachment"]; this would mean that in the $_FILES array there is no attachment item so there was no field w/ the name 'attachment' used or were not posted. and you should change your _files and files_ both to $_FILES I always use the $_FILES array when i work with file uploads. Quote Link to comment Share on other sites More sharing options...
sandbudd Posted July 5, 2008 Author Share Posted July 5, 2008 that is where it is at..are you talking about in my form..here is the code <form action="upload2.php" method="post" enctype="multipart/form-data"> <table width="350" border="0" cellpadding="1" cellspacing="1" class="box"> <tr> <td width="246"><input name="file1" type="file" /> <input name="file2" type="file" /> </td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <p> </p> </form> Quote Link to comment Share on other sites More sharing options...
ron8000 Posted July 5, 2008 Share Posted July 5, 2008 There is no file field named attachment eg. <input name="attachment" type="file" /> if the rest of the code is good then it should work after that Quote Link to comment Share on other sites More sharing options...
sandbudd Posted July 5, 2008 Author Share Posted July 5, 2008 how do i differ between the multiple upload? Quote Link to comment Share on other sites More sharing options...
ron8000 Posted July 5, 2008 Share Posted July 5, 2008 why not just loop thru the $_FILES and just run the upload function on each item... but to do this you should prob remove the hand coded field value. You pass a var to the function anywhay called $file. So maybe something along the lines of <?php $dest = 'my/upload/path'; if(isset($_FILES) && !empty($_FILES) && is_array($_FILES)) { foreach($_FILES as $file) { $upload_msg[$file['name']] = move_upload($file, $dest); } } ?> As far as that function goes... not sure but you can try this <?php function move_upload( $file, $dest, $overwrite = false ) { if ($file["error"] == UPLOAD_ERR_OK) { if(!file_exists( $dest.$file["name"] ) || $overwrite) { if(move_uploaded_file( $file["tmp_name"], $dest.$file["name"] )) { $upload_feedback .= "The file " . $file["name"] . " was successfully uploaded"; $error = FALSE; } else { $upload_feedback .= "The file " . $file["name"] . " could not be moved"; $error = TRUE; } } else { $upload_feedback .= "The file " . $file["name"] . " already exists, please check the overwrite option if you wish to replace it"; $error = TRUE; } } else { switch ($file["error"]) { case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: $upload_feedback .= "The file " . $file["name"] . " is to large to be uploaded<br />"; $error = TRUE; break; case UPLOAD_ERR_PARTIAL: $upload_feedback .= "The file" . $file["name"] . " was interrupted while uploading, please try again<br />"; $error = TRUE; break; } } return array( "error" => $error, "feedback" => $upload_feedback ); //return message plus error status } /* All of the uploaded images */ $attachment = $file['name']; /* Call the function to move the files */ move_uploaded_file($file['tmp_name'],"http://fortwayneanodizing.com/image/upload/".$file['name']) ; ?> <?php mysql_query("INSERT INTO `upload` (`attachment`) VALUES('{$attachment}')") or die(mysql_error()); ?> Quote Link to comment Share on other sites More sharing options...
sandbudd Posted July 5, 2008 Author Share Posted July 5, 2008 getting closer...here is the code <?php error_reporting(E_ALL); ?> <?php $db_host = ''; // don't forget to change $db_user = ''; $db_pwd = ''; $database = ''; $table = ''; if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database"); if (!mysql_select_db($database)) die("Can't select database"); $dest = 'upload/'; if(isset($_FILES) && !empty($_FILES) && is_array($_FILES)) { foreach($_FILES as $file) { $upload_msg[$file['name']] = move_upload($file, $dest); } } function move_upload( $file, $dest, $overwrite = false ) { if ($file["error"] == UPLOAD_ERR_OK) { if(!file_exists( $dest.$file["name"] ) || $overwrite) { if(move_uploaded_file( $file["tmp_name"], $dest.$file["name"] )) { $upload_feedback .= "The file " . $file["name"] . " was successfully uploaded"; $error = FALSE; } else { $upload_feedback .= "The file " . $file["name"] . " could not be moved"; $error = TRUE; } } else { $upload_feedback .= "The file " . $file["name"] . " already exists, please check the overwrite option if you wish to replace it"; $error = TRUE; } } else { switch ($file["error"]) { case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: $upload_feedback .= "The file " . $file["name"] . " is to large to be uploaded<br />"; $error = TRUE; break; case UPLOAD_ERR_PARTIAL: $upload_feedback .= "The file" . $file["name"] . " was interrupted while uploading, please try again<br />"; $error = TRUE; break; } } return array( "error" => $error, "feedback" => $upload_feedback ); //return message plus error status } /* All of the uploaded images */ $attachment = $file['name']; /* Call the function to move the files */ move_uploaded_file($file['tmp_name'],"http://fortwayneanodizing.com/image/upload/".$file['name']) ; ?> <?php mysql_query("INSERT INTO `upload` (`attachment`) VALUES('{$attachment}')") or die(mysql_error()); ?> get this error Notice: Undefined variable: upload_feedback in /home/anodizi/public_html/image/upload2.php on line 42 Quote Link to comment Share on other sites More sharing options...
sandbudd Posted July 5, 2008 Author Share Posted July 5, 2008 this is line 42 $upload_feedback .= "The file " . $file["name"] . " was successfully uploaded"; Quote Link to comment Share on other sites More sharing options...
ron8000 Posted July 5, 2008 Share Posted July 5, 2008 ok very easy just after your <?php $table = ''; ?> just add a <?php $upload_feedback = ''; ?> Some pointers for running in E_ALL on all the time, it is VERY strict. This is a fine example if you try to use a Var w/o defining what it is or giving it a value, it will error out... you should always use isset or empty or is_array or w/e to verify your data is what you want it to be... but enough about that let me know if that works for you. Quote Link to comment Share on other sites More sharing options...
ron8000 Posted July 5, 2008 Share Posted July 5, 2008 Then it's prob done and you can turn off error_reporting(E_ALL) just comment it out and see what happens Quote Link to comment Share on other sites More sharing options...
sandbudd Posted July 5, 2008 Author Share Posted July 5, 2008 wow now I get this Notice: Undefined variable: upload_feedback in /home/anodizi/public_html/image/upload2.php on line 58 Warning: move_uploaded_file(http://fortwayneanodizing.com/image/upload/gb.gif) [function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections in /home/anodizi/public_html/image/upload2.php on line 87 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpcjIPFN' to 'http://fortwayneanodizing.com/image/upload/gb.gif' in /home/anodizi/public_html/image/upload2.php on line 87 Quote Link to comment Share on other sites More sharing options...
ron8000 Posted July 5, 2008 Share Posted July 5, 2008 This is whre you may need to check into your PHP ini settings 1st make sure that file uploads are on... after that I'm not sure... never ran into that problem... Now it also looks like the file may already be there... did you check that? Quote Link to comment Share on other sites More sharing options...
sandbudd Posted July 5, 2008 Author Share Posted July 5, 2008 k it uploaded and populated the database but only one of the files uploaded? Quote Link to comment Share on other sites More sharing options...
sandbudd Posted July 5, 2008 Author Share Posted July 5, 2008 one file and gives this Notice: Undefined variable: upload_feedback in /home/anodizi/public_html/image/upload2.php on line 47 Quote Link to comment Share on other sites More sharing options...
ron8000 Posted July 5, 2008 Share Posted July 5, 2008 Post a <?php var_dump($_FILES); ?> So i can see what's in your $_FILES array Quote Link to comment Share on other sites More sharing options...
sandbudd Posted July 5, 2008 Author Share Posted July 5, 2008 this is what I get array(1) { ["attachment"]=> array(5) { ["name"]=> string(18) "Hawaii2008 095.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(14) "/tmp/php4ja7PN" ["error"]=> int(0) ["size"]=> int(258368) } } Notice: Undefined variable: upload_feedback in /home/anodizi/public_html/image/upload2.php on line 48 Quote Link to comment Share on other sites More sharing options...
sandbudd Posted July 5, 2008 Author Share Posted July 5, 2008 Ron did you give up on me ??? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 5, 2008 Share Posted July 5, 2008 I know why it errored like that. You can't supply the function with a path that will make PHP uses it HTTP wrapper. You need to supply a filesystem path. Quote Link to comment Share on other sites More sharing options...
sandbudd Posted July 5, 2008 Author Share Posted July 5, 2008 DarkWater could you explain please? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 5, 2008 Share Posted July 5, 2008 Sure, sorry about that. Look at this line: move_uploaded_file($file['tmp_name'],"http://fortwayneanodizing.com/image/upload/".$file['name']); You need to pass it "image/upload/{$file['name']}" instead, so it puts it on the filesystem instead of trying to open the web page. You need to supply the relative or absolute path. Understand? Quote Link to comment Share on other sites More sharing options...
sandbudd Posted July 5, 2008 Author Share Posted July 5, 2008 DarkWater I changed it to this /* All of the uploaded images */ $attachment = $file['name']; /* Call the function to move the files */ move_uploaded_file($file['tmp_name'],"upload/{$file['name']}".$file['name']) ; ?> <?php mysql_query("INSERT INTO items(attachment) VALUES('$attachment')") ; or die(mysql_error()); ?> and get this error with nothing uploading? Parse error: syntax error, unexpected T_LOGICAL_OR in /home/anodizi/public_html/image/upload2.php on line 93 this is line 93 or die(mysql_error()); 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.