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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.