PHP - how to copy a file using a page like this?

Yohanes Adi P

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

Professor Abronsius

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to copy php file multiple file name using php?

From Dev

how to copy php file multiple file name using php?

From Dev

How to copy file from hosted server to local drive using PHP

From Dev

save copy of existing remote page using php

From Dev

How to send input file data value using ajax to a php page

From Dev

How to send input file data value using ajax to a php page

From Dev

I would like to know, how to send the form values from a wordpress page to a php file

From Dev

Copy Table Value On Click To Another Page Input using PHP

From Dev

How to copy a specific file in a folder using terminal

From Dev

How to build a "file copy" RPM using cmake?

From Dev

How to copy a file to many directories using prompt?

From Dev

How to copy file to all clients using puppet?

From Dev

How to copy file to all clients using puppet?

From Dev

How to copy a file on remote machine using PySTAF

From Dev

How to build a "file copy" RPM using cmake?

From Dev

include php file in page using Javascript

From Dev

Qt - How to copy file with QFile::copy using QFileDialog?

From Dev

How to code like button in php using ajax?

From Dev

How to include a digital clock using java script in a php page without "borrowing" it from any other site just like the one given in the code?

From Dev

how to compile c++ code to display in a web page (like in PHP)

From Dev

How can I copy a file content into a temporary file in PHP?

From Dev

How to run external PHP file using Ajax and display results in same page

From Dev

How to show the image uploaded using HTML file control on a new page after form submission in PHP?

From Dev

How to create a text file using php from the parameters passed to html page in url?

From Dev

How to continuously read data from different html page into one curly bracket in JSON file using PHP?

From Dev

How to copy the page setup from worksheets to another using VBA

From Dev

How to copy element of a web page to other web pages using JavaScript

From Dev

How to copy html page

From Dev

How to Copy (and increment) Multiple Instances of a File Using Batch File

Related Related

  1. 1

    how to copy php file multiple file name using php?

  2. 2

    how to copy php file multiple file name using php?

  3. 3

    How to copy file from hosted server to local drive using PHP

  4. 4

    save copy of existing remote page using php

  5. 5

    How to send input file data value using ajax to a php page

  6. 6

    How to send input file data value using ajax to a php page

  7. 7

    I would like to know, how to send the form values from a wordpress page to a php file

  8. 8

    Copy Table Value On Click To Another Page Input using PHP

  9. 9

    How to copy a specific file in a folder using terminal

  10. 10

    How to build a "file copy" RPM using cmake?

  11. 11

    How to copy a file to many directories using prompt?

  12. 12

    How to copy file to all clients using puppet?

  13. 13

    How to copy file to all clients using puppet?

  14. 14

    How to copy a file on remote machine using PySTAF

  15. 15

    How to build a "file copy" RPM using cmake?

  16. 16

    include php file in page using Javascript

  17. 17

    Qt - How to copy file with QFile::copy using QFileDialog?

  18. 18

    How to code like button in php using ajax?

  19. 19

    How to include a digital clock using java script in a php page without "borrowing" it from any other site just like the one given in the code?

  20. 20

    how to compile c++ code to display in a web page (like in PHP)

  21. 21

    How can I copy a file content into a temporary file in PHP?

  22. 22

    How to run external PHP file using Ajax and display results in same page

  23. 23

    How to show the image uploaded using HTML file control on a new page after form submission in PHP?

  24. 24

    How to create a text file using php from the parameters passed to html page in url?

  25. 25

    How to continuously read data from different html page into one curly bracket in JSON file using PHP?

  26. 26

    How to copy the page setup from worksheets to another using VBA

  27. 27

    How to copy element of a web page to other web pages using JavaScript

  28. 28

    How to copy html page

  29. 29

    How to Copy (and increment) Multiple Instances of a File Using Batch File

HotTag

Archive