-1

I was trying to upload a csv file in php. I also want it to be able to accept any type of file.

This is the code I'm using, however, it always enters the, "Could not copy file!" condition.

HTML code

<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>

PHP code

<?php
if( $_FILES['file']['name'] != "" )
{
   copy( $_FILES['file']['name'], "/var/www/html/uploads" ) 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>

I have checked that the uploads folder and the html as well as php files are of permission 777, so they should be able to upload these files

EDIT I have removed a lot of code from the php file and tried to make it upload(copy) a hardcoded file path from the filesystem.

What I observe is that, if I just run php upload.php from the command line, it works and "uploads" the file from the Downloads onto the Desktop. However,the same upload.php, if run from the browser and via index.html, on one of my servers, it just says, "Could not copy file!". On another server, it downloads the upload.php file. Basically, the same script works from the command line. Doesn't work from the browser.

(I don't see any error message etc on the browser console)

index.html

<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>

upload.php

<?php
$a = 2;
$b = 1;

if( $a > $b )
{
 copy( "/home/PepperBoy/Downloads/myfile.csv", "/home/PepperBoy/Desktop/myfile.csv" ) 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>
1
  • 1
    I'm not going to waste time explaining this. Take a look at the PHP section on Handling File Uploads - everything you need is there.
    – user1864610
    Apr 5, 2015 at 9:52

2 Answers 2

0

Replace this with your same line of code.

copy( $_FILES['file']['tmp_name'], "/var/www/html/uploads" ) or die( "Could not copy file!");
3
  • Thanks Iffi. But, noo I still don't get it. I get the same output, "Could not copy file!" I have also changed the path from /var/www/html/uploads to a local folder and tried, so as to rule out the possibility of it being a permission problem, but no I'm not able to upload the file.
    – tubby
    Apr 5, 2015 at 17:29
  • Like I was telling Mike below, I also commented out the copy line of code and just did echo($_FILES['file']['tmp_name']); I got the the name of the file printed as /tmp/php03eblp and it also printed out the Uploaded File Info part of the php code , which means somehow the copy is not working?
    – tubby
    Apr 5, 2015 at 17:41
  • Ahh try the following command. This is what I am using in my projects. $file = $target_path . basename($_FILES['file']['name']); if (move_uploaded_file($_FILES['file']['tmp_name'], $file)){ chmod(basename($_FILES['file']['name']), 0777);}
    – Iffi
    Apr 6, 2015 at 4:21
0

The name value in the $_FILES array doesn't contain the path to the file. Use tmp_name instead. You also need to give the copied file a name not just the destination directory. Also move the file rather than copy it or you will end up cluttering up the tmp directory

move_uploaded_file( $_FILES['file']['tmp_name'], "/var/www/html/uploads/". $_FILES['file']['name'] ) 
or die( "Could not copy file!");
4
  • thanks. But I tried that, still not able to upload the file. I'm running from /var/www/html/nameofproject . I also tried commenting out the line which copies the file and just did echo($_FILES['file']['tmp_name']);; I got the the name of the file printed as /tmp/php03eblp, which means somehow the copy is not working?
    – tubby
    Apr 5, 2015 at 17:39
  • noo :( not yet. Thanks for helping out, I really appreciate. But I tried doing a simple exec("ls > outputfile") and I'm not able to perform this as well. When I change it to exec("sudo ls > outputfile") I get the output, though I have to execute my php script as sudo myscript.php in spite of it having 777 permissions. Weird! Is there a way to execute the copy line of code you showed above as a super user inside the code? That could solve my problem.
    – tubby
    Apr 5, 2015 at 18:56
  • I updated further. There is no reason to use exec and sudo as long as the uploads directory is writable it will work. Make sure you have properly copied the answer and turn on error reporting as you should get a warning about it failing Apr 5, 2015 at 19:05
  • Sure, let me try out these and get back.
    – tubby
    Apr 5, 2015 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.