Gazz1982 Posted June 7, 2008 Share Posted June 7, 2008 I have 2 scripts how do I combine them as they wont work like this? I want an image to be uploaded to the uploads/ directory and the uploads/thumbs/ directory <?php $uploaddir1 = "uploads/"; $uploadfile1 = $uploaddir1 . $_FILES['upfile']['name']; if (move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile1)) { print("File upload success"); } else { print("Failed"); } $uploaddir2 = "uploads/thumbs/"; $uploadfile2 = $uploaddir2 . $_FILES['upfile']['name']; if (move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile2)) { print("<br />Thumbnail upload success"); } else { print("<br />Thumbnail Failed"); } ?> Quote Link to comment Share on other sites More sharing options...
.josh Posted June 7, 2008 Share Posted June 7, 2008 it doesn't work because you are moving the file; it can't be in two places at once. You move it, and then it's not in the original place anymore, so when you try to move it a 2nd time, move_uploaded_file returns false because it wasn't there to move. You need to first make a copy of the file and then move one to one directory and then the other to the other. Quote Link to comment Share on other sites More sharing options...
Gazz1982 Posted June 7, 2008 Author Share Posted June 7, 2008 gave this error File upload success Warning: copy(uploads/Array) [function.copy]: failed to open stream: No such file or directory in /var/www/uploader.php on line 20 failed to copy uploads/Array... <?php $uploaddir1 = "uploads/"; $uploaddir2 = "uploads/thumbs/"; $uploadfile1 = $uploaddir1 . $_FILES['upfile']['name']; if (move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile1)) { print("File upload success"); } else { print("Failed"); } $file = $uploaddir1 . $_FILES['upfile']; $newfile = $uploaddir2 . $_FILES['upfile']; if (!copy($file, $newfile)) { echo "failed to copy $file...\n"; } am I close? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted June 7, 2008 Share Posted June 7, 2008 Try the following (untested) code: <?php $uploaddir1 = 'uploads/'; $uploaddir2 = 'uploads/thumbs/'; $uploadfile1 = $uploaddir1 . $_FILES['upfile']['name']; $uploadfile2 = $uploaddir2 . $_FILES['upfile']['name']; if(file_exists($_FILES['upfile']['tmp_name'])) { if( copy($_FILES['upfile']['tmp_name'], $uploadfile2) && move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile1)) { echo 'Image uploaded successfully'; } else { echo 'Error image uploaded unsuccessfully'; } } ?> Quote Link to comment Share on other sites More sharing options...
.josh Posted June 7, 2008 Share Posted June 7, 2008 okay see now I just told you that you can't move a file that's already moved. The same thing applies to trying to copy a file that's already moved. Your error just told you the same thing. You need to copy the file first, then move move each one. Quote Link to comment Share on other sites More sharing options...
Gazz1982 Posted June 7, 2008 Author Share Posted June 7, 2008 I'm nearly there! $uploaddir1 = 'uploads/'; $uploaddir2 = "uploads/thumbs/"; $uploadfile1 = $uploaddir1 . $_FILES['upfile']['name']; $uploadfile2 = $uploaddir2 . $_FILES['upfile']['name']; if (!file_exists('$uploaddir1 . $uploadfile1')) { copy($_FILES['upfile']['tmp_name'],$uploadfile1); copy($_FILES['upfile']['tmp_name'],$uploadfile2); }else{ echo "file already exists";}; ?> the file_exists should be checking the upload_directory/file_name if it doesn't exist then copy else don't copy - this doesn't seem to be working quite right - any obvious errors? 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.