padams Posted November 6, 2008 Share Posted November 6, 2008 I need to upload two files, a main image and a thumbnail, and would like to use the same form. I'm using the code below, but for some reason when it runs I get a strange issue. The first file uploads fine, but the second one ends up with the first file name appended on the start of its filename. For example, I uploaded two files, mainpic.jpg and thumbnail.jpg, and the resulting filenames on the server are mainpic.jpg and mainpic.jpgthumbnail.jpg. How do I stop this happening? $target_path = "Wallpapers/"; $name = $_POST['name']; $wallpaper = $_FILES['uploadedfile']['name']; /* Add the original filename to our target path. Result is "uploads/filename.extension" */ $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { $result = "Wallpaper has been successfully uploaded"; } else { $result = "There has been an error. Try again later"; } $thumbnail = $_FILES['uploadedthumbnail']['name']; $target_path = $target_path . basename( $_FILES['uploadedthumbnail']['name']); if(move_uploaded_file($_FILES['uploadedthumbnail']['tmp_name'], $target_path)) { $thumbnailresult = "Thumbnail has been successfully uploaded"; } else { $thumbnailresult = "There has been an error. Try again later"; } Link to comment https://forums.phpfreaks.com/topic/131562-uploading-multiple-files/ Share on other sites More sharing options...
JasonLewis Posted November 6, 2008 Share Posted November 6, 2008 Because you are using the $target_path which was set by the first image. $target_path_first = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path_first)) { $result = "Wallpaper has been successfully uploaded"; } else { $result = "There has been an error. Try again later"; } $thumbnail = $_FILES['uploadedthumbnail']['name']; $target_path_second = $target_path . basename( $_FILES['uploadedthumbnail']['name']); if(move_uploaded_file($_FILES['uploadedthumbnail']['tmp_name'], $target_path_second)) { $thumbnailresult = "Thumbnail has been successfully uploaded"; } else { $thumbnailresult = "There has been an error. Try again later"; } See how I have given the two image target paths different names. That way they both use the folder location that you have specified and the second image doesn't have the target path of the first image at the start of it. Link to comment https://forums.phpfreaks.com/topic/131562-uploading-multiple-files/#findComment-683321 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.