BandonRandon Posted April 9, 2009 Share Posted April 9, 2009 Hello, This code worked a few revisions ago and I don't know what I did. What I'm trying to do is pull the value from <input name="upload_project_images[]" type="file" /><br/> and loop though changing the filename to something like "originalfilename_pimg_rand3425" Ideally it would be nice to change "pimg" to "pimg1, pimg2 ext" I believe I can use an $i++ and possibly a for loop may be a better option than a for each loop. To recap, trying to get the MYSQL to loop though and add the variables into the DB once this is done then I'll be able to "move" the uploaded files. foreach($_FILES['upload_project_images']['name'] as $upload_project_images) { $upload_project_images_pathinfo = pathinfo($upload_project_images); $upload_project_images_name = $upload_project_images_pathinfo['filename'] . "_" . $rand . '.' . $upload_project_images_pathinfo['extension']; $p_image_query = "INSERT INTO " . IMAGE_TABLE . "(i_name,i_type,i_project_id) VALUES ($upload_project_images_name','3','$project_id')"; $result=mysql_query($p_image_query); } Quote Link to comment https://forums.phpfreaks.com/topic/153251-multiple-file-rename-and-insert-into-db/ Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 Not sure where $project_id comes from but you can try this: $i = 1; foreach($_FILES['upload_project_images'] AS $upload_project_images) { $upload_project_images_pathinfo = pathinfo($upload_project_images); $upload_project_images_name = $upload_project_images_pathinfo['filename'] . "_".$i.".". $upload_project_images_pathinfo['extension']; $p_image_query = "INSERT INTO " . IMAGE_TABLE . "(i_name, i_type, i_project_id) VALUES ($upload_project_images_name', '3', '$project_id')"; $result=mysql_query($p_image_query) or die(mysql_error()); $i++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/153251-multiple-file-rename-and-insert-into-db/#findComment-805084 Share on other sites More sharing options...
BandonRandon Posted April 9, 2009 Author Share Posted April 9, 2009 thanks the unfortunately didn't work $project_id comes from the previous query and is used to store the id of the project that the image is associated with. $project_id_row = mysql_fetch_array(mysql_query("SELECT p_id FROM ".PROJECT_TABLE ." ORDER BY p_id DESC LIMIT 1")); $project_id = $project_id_row['p_id']; here are the errors I get Warning: pathinfo() expects parameter 1 to be string, array given in actions.php on line 149 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '', '3', '26')' at line 1 looks like $upload_project_images_name isn't being sent. Quote Link to comment https://forums.phpfreaks.com/topic/153251-multiple-file-rename-and-insert-into-db/#findComment-805096 Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 This line is missing a single quote before the variable: VALUES ('$upload_project_images_name', Change this back to: foreach($_FILES['upload_project_images']['name'] as $upload_project_images) { Quote Link to comment https://forums.phpfreaks.com/topic/153251-multiple-file-rename-and-insert-into-db/#findComment-805098 Share on other sites More sharing options...
BandonRandon Posted April 9, 2009 Author Share Posted April 9, 2009 thanks maq! I love this forum always knowledgeable people who are fairly quick to respond! I'm a little disappointed that i forgot a stupid quote. I was closer than i thought! Thanks again for your help! Quote Link to comment https://forums.phpfreaks.com/topic/153251-multiple-file-rename-and-insert-into-db/#findComment-805104 Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 thanks maq! I love this forum always knowledgeable people who are fairly quick to respond! I'm a little disappointed that i forgot a stupid quote. I was closer than i thought! Thanks again for your help! YW It's always the little things that cause the problems. They're always so little that you overlook them and complicate the problem more than it is. Glad it's working. Quote Link to comment https://forums.phpfreaks.com/topic/153251-multiple-file-rename-and-insert-into-db/#findComment-805110 Share on other sites More sharing options...
BandonRandon Posted April 9, 2009 Author Share Posted April 9, 2009 using the code above how can i loop through and move each of the files? I tried running another foreach loop inside of the original loop. I'm so lost on this and i know it should be easy. foreach($_FILES['upload_project_images']['tmp_name'] as $upload_project_temp_name) { move_uploaded_file ($upload_project_temp_name, "$upload_project_images_path"."$upload_project_images_name"); } Quote Link to comment https://forums.phpfreaks.com/topic/153251-multiple-file-rename-and-insert-into-db/#findComment-805205 Share on other sites More sharing options...
BandonRandon Posted April 9, 2009 Author Share Posted April 9, 2009 ok so this apparently will upload one file to the correct location but not all of them. Quote Link to comment https://forums.phpfreaks.com/topic/153251-multiple-file-rename-and-insert-into-db/#findComment-805232 Share on other sites More sharing options...
BandonRandon Posted April 9, 2009 Author Share Posted April 9, 2009 Sorry to bump this but does anyone have any ideas? I have been trying a variety of things but none of them are working if someone even wants to point me to an article i would be willing to read it. Quote Link to comment https://forums.phpfreaks.com/topic/153251-multiple-file-rename-and-insert-into-db/#findComment-805881 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.