-3

this is error message I'm trying to fix the copy function does not run. This my code:

<?php
if( $_FILES['file']['name'] != "" ) {

    copy( $_FILES['file']['name'], "php5/image" ) or die( "Could not copy file!");

} else {
    die("No file specified!");
}
?>

<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name']; ?>
<li>File size: <?php echo $_FILES['file']['size']; ?> bytes
<li>File type: <?php echo $_FILES['file']['type']; ?>
</ul>
</body>
</html>

Why it doesn't work? How to fix it?

I'm getting

Warning: copy(feel_like_doraemon.jpg): failed to open stream: No such file or directory

4 Answers4

0

As ['name'] is name of file and not path to file after upload instead of ['name'] try use ['tmp_name']

copy( $_FILES['file']['tmp_name'], "php5/image" ) or die( "Could not copy file!");
Marek Maszay
  • 1,537
  • 1
  • 9
  • 11
0

This is because uploaded files are not stored on the server using their original name, they are given a temp filename which is in the tmp_name index. Furthermore you should use move_uploaded_file instead of copy for security.

Also your destination path needs to exist.

Geoffrey
  • 10,843
  • 3
  • 33
  • 46
0

As per your code you have to use move upload file function instead of copy.

Do as mentioned below.
<?php
if( $_FILES['file']['name'] != "" ) {

    $tmp_name = $_FILES["file"]["tmp_name"];
    $name = basename($_FILES["file"]["name"]);
    move_uploaded_file($tmp_name, "php5/image/".$name)or die( "Could not copy file!");
    //copy( $_FILES['file']['name'], "php5/image" ) or die( "Could not copy file!");

} else {

    die("No file specified!");

}
?>
Priyank
  • 470
  • 2
  • 11
  • do NOT advise people to give 777 permissions on directories! Learn about file permissions and how they work before you tell people this dangerous advice! See: https://stackoverflow.com/questions/11271596/how-will-a-server-become-vulnerable-with-chmod-777 – Geoffrey Aug 14 '17 at 06:33
  • but you have to use move upload file instead of copy it will solve your problem – Priyank Aug 14 '17 at 06:47
  • I down voted you because of your ill advice, until you remove it the down vote stays. – Geoffrey Aug 14 '17 at 09:11
0

When uploading files you should use move_uploaded_file to actually store the file in your chosen directory. Also, using a full path rather than a relative path has always worked better for me - change the applicable line to suit your environment.

<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_FILES['file'] ) ){
    try{
        $obj=$_FILES['file'];
        $name=$obj['name'];
        $tmp=$obj['tmp_name'];

        $dir=$_SERVER['DOCUMENT_ROOT'] . '/php5/image';
        if( !realpath( $dir ) )throw new Exception( sprintf( 'unable to find target directory %s', $dir ) );

        $target="{$dir}/{$name}";

        if( is_uploaded_file( $tmp ) && move_uploaded_file( $tmp, $target ) ){
            /* OK */
            @unlink( $tmp );
        } else {
            /* error */
        }
    }catch(Exception $e ){
        exit( $e->getMessage() );
    }
}


?>

<html>
    <head>
        <title>Uploading Complete</title>
    </head>
    <body>
        <h2>Uploaded File Info:</h2>
        <ul>
            <li>Sent file: <?php echo $_FILES['file']['name']; ?>
            <li>File size: <?php echo $_FILES['file']['size']; ?> bytes
            <li>File type: <?php echo $_FILES['file']['type']; ?>
        </ul>
    </body>
</html>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • doesn't `move_uploaded_file` move the file out, which makes your call to `unlink` pointless? Suppressing errors with an `@` is never advisable. – Geoffrey Aug 14 '17 at 06:38
  • Good stuff - you probably should make the above a little more resilient by adding additional checks to verify the type and size of the file before saving - but glad it works now for you – Professor Abronsius Aug 14 '17 at 06:38
  • belt n braces... using the `@` before `unlink` suppresses any such error – Professor Abronsius Aug 14 '17 at 06:39
  • @RamRaider If the system is so broken that `move_uploaded_file` fails you have much bigger problems to worry about. The additional call to `unlink` is absolutely useless, adds overhead, and potentially could hide a real error. – Geoffrey Aug 14 '17 at 06:42
  • Okay i will try to upgrade my upload file :D – Yohanes Adi P Aug 14 '17 at 06:44