Jump to content

combining 2 scripts


Gazz1982

Recommended Posts

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

?>

Link to comment
https://forums.phpfreaks.com/topic/109132-combining-2-scripts/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/109132-combining-2-scripts/#findComment-559791
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/109132-combining-2-scripts/#findComment-559808
Share on other sites

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

?>

 

Link to comment
https://forums.phpfreaks.com/topic/109132-combining-2-scripts/#findComment-559820
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/109132-combining-2-scripts/#findComment-559870
Share on other sites

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.