zkn
zkn

Reputation: 63

Upload image on multiple folder php

I need to upload image into two different folders there is my code On first folder it is moving but on 2nd folder it generate exception that could not move 2nd file

$target_path = "uploads/";
$target_path = $target_path . basename($_FILES['image']['name']);


$target_path1 = "thumbnails/";
$target_path1 = $target_path1 . basename($_FILES['image']['name']);

try {
    //throw exception if can't move the file
 if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
        throw new Exception('Could not move file');
    }
 if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path1))
    {
       throw new Exception('Could not move 2nd file');
    }

Upvotes: 2

Views: 3022

Answers (3)

Saty
Saty

Reputation: 22532

move_uploaded_file() has moved to the file to your $target_path path. So, There is nothing in your temp , for second time you use copy(). command to upload it.

if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
    throw new Exception('Could not move file');
}
if(!copy ( $target_path , $target_path1 ))
        {  
          throw new Exception('Could not move 2nd file');
        }

Upvotes: 4

alamnaryab
alamnaryab

Reputation: 1484

uploading to 2nd folder

if (!move_uploaded_file($target_path, $target_path1)){
     throw new Exception('Could not move 2nd file');
}

Upvotes: 0

Disha V.
Disha V.

Reputation: 1864

It would remove temp file ($_FILES['image']['tmp_name']) once you uploaded. You have to copy that file from first upload.

$success=true;
if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
    throw new Exception('Could not move file');
    $success=false;
}
if($success) {
    if (!move_uploaded_file($target_path, $target_path1))
    {
        throw new Exception('Could not move 2nd file');
    }
}

Upvotes: 1

Related Questions