php file upload failed - php

I followed every tutorial in the internet that I find to upload a file. But still, It failed. It gave me this error:
Warning: move_uploaded_file(/var/www/projects/upload/TASK.txt): failed to open stream: No such file or directory in /var/www/projects/test/upload.php on line 6 Warning: move_uploaded_file(): Unable to move '/tmp/phpjr2JJA' to '/var/www/projects/upload/TASK.txt' in /var/www/projects/test/upload.php on line 6 Something went wrong
index.html
<head>
<title></title>
</head>
<body>
<form method="POST" action="upload.php" enctype="multipart/form-data">
<input type="file" name="upload" ><br />
<input type="hidden" name="MAX_FILE_SIZE" value="1024" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
upload.php
<?php
$target_path = $_SERVER['DOCUMENT_ROOT'] . "/upload/";
$target_path = $target_path . basename( $_FILES['upload']['name'] );
if ( move_uploaded_file($_FILES['upload']['tmp_name'], $target_path) ) {
echo "has been uploaded";
} else {
echo "Something went wrong";
}
Can you help me and point out where I went wrong? I'm using ubuntu 12.04 and also I tried to change the permission for the /upload folder to 755 and checked the file_upload in php.ini is ON
Any help would be much appreciated. Thanks!

$_SERVER['DOCUMENT_ROOT'] gives /var/www/projects/upload.. as output
/ in the start is cause of error
Hence
try with relative path
<?php
$target_path = "upload/";
$target_path = $target_path . basename( $_FILES['upload']['name'] );
if ( move_uploaded_file($_FILES['upload']['tmp_name'], $target_path) ) {
echo "has been uploaded";
} else {
echo "Something went wrong";
}
?>
worked for me

File & Dir permissions...
Add in your FTP Program 666 to the files and dirs what you need to write.

I think ur file upload location incorrect
index.php
<html>
<head>
<title>Upload your file</title>
</head>
<body>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="Upload" value="Submit">
</form>
</body>
upload.php
<?php
$target="give the path you want to store the file/";
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$target. $_FILES["file"]["name"]);
}
?>

Related

There was an error uploading the file, please try again!Error Code:6

We have a website using PHP 5.2 and it is hosted in Windows Plesk Server. We are now having issue in uploading any files via PHP. When we try to do so we are getting following error.
There was an error uploading the file, please try again!Error Code:6
upload_tmp_dir has local value "C:\Inetpub\vhosts\xxxxxx.xxx\httpdocs\tmp" and master value "C:\Windows\Temp" .
Can anyone suggest what should be the permission of these folders or do we need to check something else to fix this upload issue via PHP?
Here are the scripts I have used to test the Upload.
Script 1
<!DOCTYPE html>
<html>
<head>
<title>Upload your files</title>
</head>
<body>
<form enctype="multipart/form-data" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?PHP
if(!empty($_FILES['uploaded_file']))
{
$path = "newupload/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!Error Code:". $_FILES['uploaded_file']["error"];;
}
}
?>
Script 2
<?php
echo '<form action="" method="post" enctype="multipart/form-data" name="uploader" id="uploader">';
echo '<input type="file" name="file" size="50"><input name="_upl" type="submit" id="_upl" value="Upload"></form>';
if( $_POST['_upl'] == "Upload" ) {
if(#copy($_FILES['file']['tmp_name'], $_FILES['file']['name'])) { echo '<b>Upload SUKSES !!!</b><br><br>'; }
else { echo '<b>Upload GAGAL !!!</b><br><br>'; }
}
?>
Script 1 gave the error "There was an error uploading the file, please try again!Error Code:6" . Script 2 showed upload success. But uploaded file was missing.

Simple File upload code in PHP

This issue might have been discussed multiple times but I wanted a simple PHP script to upload a file, without any separate action file and without any checks. Below is my written code:-
<html>
<head>
<title>PHP Test</title>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="upload file" name="submit">
</form>
</head>
<body>
<?php echo '<p>FILE UPLOAD</p><br>';
$tgt_dir = "uploads/";
$tgt_file = $tgt_dir.basename($_FILES['fileToUpload']['name']);
echo "<br>TARGET FILE= ".$tgt_file;
//$filename = $_FILES['fileToUpload']['name'];
echo "<br>FILE NAME FROM VARIABLE:- ".$_FILES["fileToUpload"]["name"];
if(isset($_POST['submit']))
{
if(file_exists("uploads/".$_FILES["fileToUpload"]["name"]))
{ echo "<br>file exists, try with another name"; }
else {
echo "<br>STARTING UPLOAD PROCESS<br>";
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $tgt_file))
{ echo "<br>File UPLOADED:- ".$tgt_file; }
else { echo "<br>ERROR WHILE UPLOADING FILE<br>"; }
}
}
?>
</body>
</html>
I saved it in /var/www/html/phps/ location. But everytime I try to upload the file, I get ERROR WHILE UPLOADING FILE error. What am I doing wrong here. P.S. I have no previous experience of PHP, I just started with bits & pieces from internet.
Thanks
kriss
<?php
$target_dir = "uploads/";
$target_file = $target_dir .
basename($_FILES["fileToUpload"]["name"]);
if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ".basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} 
else {
echo "Sorry, there was an error uploading your file.";
}
?>
I hope that this thing will work and this is what you are in need of.
<?php
$name = $_POST['name'];
$image = $_FILES['fileToUpload']['name'];
$tempname = $_FILES['fileToUpload']['tmp_name'];
move_uploaded_file($tempname, "foldername/$image");?>

How to upload a file to a specific folder using php?

My code is as follows..Here when I upload a file named Koala.jpg,then the warning is shown--Warning: copy(Koala.jpg): failed to open stream: No such file or directory in E:\xampp\htdocs\Forum\upload.php on line 4
Could not copy file!..Please solve the problem urgently..
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>
***upload.php***
<?php
if( $_FILES['file']['name'] != "" )
{
copy( $_FILES['file']['name'], "/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>
Try this
<?php
if( $_FILES['file']['name'] != "" ) {
$path=$_FILES['file']['name'];
$pathto="/uploads/".$path;
move_uploaded_file( $_FILES['file']['tmp_name'],$pathto) or die( "Could not copy file!");
}
else {
die("No file specified!");
}
?>
Source :
http://php.net/manual/en/features.file-upload.post-method.php
http://www.tizag.com/htmlT/htmlupload.php
Replace copy function by following
$destFile = "/root/mysite/upload_files/".$_FILES['file']['name'];
move_uploaded_file( $_FILES['file']['tmp_name'], $destFile );
Note the location is a filesystem destination not a URL. Also the filesystem path is relative to the highest root rather than the website home URL.

File upload.php not working

I have following code.
index.php
<!DOCTYPE html>
<html>
<title>File Sharing</title>
<body>
<h1>File Sharing</h1><br/><br/>
<form action="upload.php" method="get" enctype="multipart/form-data">
Select File to Share:
<input type="file" name="file" id="file"/>
<button type="submit" name="submit" value="submit" id="submit" >Share</button>
</form>
<br/>
<p> or view shared files </p>
</body>
</html>
and upload.php
<?php
if(isset($_FILES['file'])){
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
// Check file size
if ($_FILES["file"]["size"] > 50000000000000000000000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["file"]["name"]). " has been shared.<br/>";
echo "<br/><p><a href='index.php'>Go Back</a>or<a href='download.php'>View Shared files</a></p>";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
else{
echo"error uploading";
}
?>
following the above my files do not get uploaded. and gives "error uploading" i got many questions for this problem i tried all but none solves my problem. What's the error???
You are using the wrong method for your form. You can't "GET" data to your server. You must "POST" it.
http://bytes.com/topic/php/insights/664241-using-html-forms-pass-data-php
Scott
My friend you should replace your GET method with POST
otherwise code working fine
<form action="upload.php" method="post" enctype="multipart/form-data">
Select File to Share:
<input type="file" name="file" id="file"/>
<button type="submit" name="submit" value="submit" id="submit" >Share</button>
</form>

PHP Uploading file unsuccessful

I'm trying to upload a file to my local server, but it keeps being unsuccessful.
All my files are inside /var/www/html/
However I made a folder called uploads in the html folder, and I changed its permissions to 777 (what I took on average from searching was the best for my needs)
this is my code:
index.html
<!DOCTYPE html>
<html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
upload.php
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES['fileToUpload']['name']);
echo "Target File: " . $target_file . "<br />";
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
Your Input file is
<input name="uploadedfile" type="file" />
so change $_FILES['fileToUpload']['name'] to $_FILES['uploadedfile']['name']
$_FILES['uploadedfile']['name'] Must have the value of Name attribute of your file field
You didn't set the variable
$target_path
you meant but not used
$target_file
instead.
Try This:
index.html
<!DOCTYPE html>
<html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
upload.php
if(isset($_FILES["uploadedfile"]["type"]) && ($_FILES["uploadedfile"]["size"] < 5000000)){
$sourcePath = $_FILES['uploadedfile']['tmp_name'];
$file = $_FILES['uploadedfile']['name'];
$targetPath = "/uploads/".$file;
if(move_uploaded_file($sourcePath,$targetPath)){
echo "The file: ".$_FILES['uploadedfile']['name']." has been uploaded";
}else{
echo "Looks like it failed.";
}
}else{
echo "You forgot to select a file, or the file size is too large.";
}
So what this does is checks if a file exists and checks if it's smaller than 5MB. If so it moves on to the upload part.

Categories