Jump to content

Copy Function


phpcoder2013

Recommended Posts

I need to modify the script to additionally do the following:

Once the image is on the server, I want it to duplicate itself, then,

rename the new duplicated file to a variable name.

(Let's say $TEST . '.jpg' where $TEST = TESTING for this example)
 

 

My current piece of code:


<?php
//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 350000Kb
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && 
    ($_FILES["uploaded_file"]["size"] < 350000000)) {
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__) . '/Uploads/' . date('Y_m_d-H:i:s') . '-' . $filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
           echo "It's done! The file has been saved as: ".$newname;
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
      }
  } else {
     echo "Error: Only .jpg images under 350000Kb are accepted for upload";
  }
} else {
 echo "Error: No file uploaded";
}
?>



 

 This following piece of code might assist you:


<?php
$file = 'one.jpg';
$newfile = 'one_thumb.jpg';
 
if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
?>

 

 

PS: I am quite new to PHP. (Introduced to it only about 3 days ago)

Link to comment
Share on other sites

I think it copies from one location to another then renames it.

 

Then, we will have a big mess :)

 

 

PS: Rename the old file with rename php function, then copy this new file to get a duplicate name to other directory.

Edited by jazzman1
Link to comment
Share on other sites

Does somebody try to move uploaded temp file to two different directories on the server. Or...when this function moved the temp file, the file has been deleted by php forever. 

 

Never tested.

 

Something like:

$tmp_name = $_FILES["pictures"]["tmp_name"]

move_uploaded_file($tmp_name, "$uploads_dir1/$name");

move_uploaded_file($tmp_name, "$uploads_dir2/$name");
Link to comment
Share on other sites

Then, we will have a big mess :)

 

 

PS: Rename the old file with rename php function, then copy this new file to get a duplicate name to other directory.

 

Just tested the code and worked as expected.

 

Copied example.txt to example.txt.bak in the same directory.

 

Can you tell me why I will have a big mess?

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.