Jump to content

multiple file rename and insert into DB


BandonRandon

Recommended Posts

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); 
           
} 

Link to comment
https://forums.phpfreaks.com/topic/153251-multiple-file-rename-and-insert-into-db/
Share on other sites

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++;
} 
?>

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.

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.

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");
} 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.