Reputation: 39
The html file
<html>
<head><title> Form Uploading </title></head>
<body>
<h3>File upload </h3>
Select a File <BR />
<Form action ="upload.php" method = "post" enctype="multipart/form-data">
<input type="file" name ="file" sieze = "50" >
<input type ="submit" value = "Upload File">
</form>
<body>
</html>
The php file
<?php
if($_FILES[ 'file'][ 'name' ] != ""){
copy ( $_FILES[ 'file'][ 'name' ], "C:\Users\Acasa\Desktop".$_FILES[ 'file'][ 'name' ]) ;#or
#die( "Could not copy file!");
echo $_FILES[ 'file'][ 'name' ];
}else{
echo "Sent File".$_FILES[ 'file' ][ 'name']."<BR />";
echo "Size File".$_FILES[ 'file' ][ 'size']."<BR />";
echo "Type File".$_FILES[ 'file' ][ 'type']."<BR />";
}
?>
Both are in the same dir.
I want to try the code from tutorialspoint.com but for some reason it`s not working... I want to copy the uploaded file into another dir they used function copy and not move_uploaded_file
Any suggestion why it`s not working?
Upvotes: 0
Views: 11118
Reputation: 29
It is working. You should use the move_uploaded_file() function instead of copy() :
Upvotes: 0
Reputation: 39
<?php
if($_FILES[ 'file'][ 'name' ] != ""){
move_uploaded_file ( $_FILES[ 'file'][ 'tmp_name' ], "C:\Users\Acasa\Desktop".$_FILES[ 'file'][ 'name' ]) or
die( "Could not copy file!");
}else{
die("no file found");
}
echo "Sent File: ".$_FILES[ 'file' ][ 'name']."<BR />";
echo "Size File: ".$_FILES[ 'file' ][ 'size']."<BR />";
echo "Type File: ".$_FILES[ 'file' ][ 'type']."<BR />";
?>
Upvotes: 0
Reputation: 643
You should use the move_uploaded_file() function instead of copy() :
<?php
if ( move_uploaded_file ( $_FILES["file"]["tmp_name"] ,
"YOUR_PATH".$your_file_name ) )
echo "Download completed";
else
echo "Error";
?>
Don't forget to check format, size, etc.. before.
Upvotes: 2