pcoder
pcoder

Reputation: 411

I am not able to upload file using php?

I am getting the following warning

Warning: copy(file.txt) [function.copy]: failed to open stream: No such
file or directory in /users/abc/pantry/public_html/index.php on line 5
Could not copy file!

<?php

if ($_FILES['file']['name'] != "") {
    copy($_FILES['file']['name'], "/users/abc/pantry/public_html/temp/") or die("Could not copy file!");
} else {
    die("No file specified!");
}
?>

Even though the folder temp is present with 777 permission .

Upvotes: 1

Views: 86

Answers (4)

Vagabond
Vagabond

Reputation: 897

Change the line with the copy function to

copy( $_FILES['file']['tmp_name'], "/users/abc/pantry/public_html/temp/" ) or
          die( "Could not copy file!");

Upvotes: 2

Barif
Barif

Reputation: 1552

You need specify file name here:

"/users/abc/pantry/public_html/temp/" . $_FILES['file']['name']

Also you should use absolute path to uploaded file:

$_FILES['file']['tmp_name']

Upvotes: 3

Lorenzo Marcon
Lorenzo Marcon

Reputation: 8179

Try:

copy( $_FILES['file']['tmp_name'], "/users/abc/pantry/public_html/temp/{$_FILES['file']['name']}" )

In fact:

$_FILES['file']['tmp_name'] contains the temporary name of the file on the server;

$_FILES['file']['name'] contains the original name of the uploaded file.

I suggest you to take a look here: http://www.php.net/move_uploaded_file

Update: as suggested by Pitchinnate, you should also set up some filename checks to avoid file overwriting. Additionally, it's not a very good idea to keep permissions to 777

Upvotes: 1

martinezjc
martinezjc

Reputation: 3555

As it say Barif you have to especify the new new of the file, here is the example from php official documentation

<?php
$archivo = 'ejemplo.txt';
$nuevo_archivo = 'ejemplo.txt.bak';

if (!copy($archivo, $nuevo_archivo)) {
    echo "Error al copiar $archivo...\n";
}
?>

Upvotes: 1

Related Questions