How to upload a file to a specific folder using php? - 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.

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.

Errors at image after set my web on public

I got errors after setup my website on public and when I finished I tried to upload the image and get errors. Is this because my website doesn't use database?
Warning: move_uploaded_file(uploads/Capture.PNG): failed to open stream: No such file or directory in /storage/ssd5/873/2077873/public_html/upload.php on line 28
Warning: move_uploaded_file(): Unable to move '/storage/ssd5/873/2077873/tmp/phpuRC82y' to 'uploads/Capture.PNG' in /storage/ssd5/873/2077873/public_html/upload.php on line 28
My code:
<html>
<head>
<title>image sharer</title>
<style>
footer {
font-family: sans-serif;
position:absolute;
bottom:0;
width:100%;
}
</style>
</head>
<body>
<h1><font color="green">#img</font><font color="blue">serv</font></h1>
<form action="upload.php" method="post" enctype="multipart/form-data">
image selector :<input type="file" name="image"><br/><br/>
<input type="submit" name="upload" value="upload">
</form>
<?php
if(isset($_POST['upload'])){
$image_name = $_FILES['image']['name'];
$image_type = $_FILES['image']['type'];
$image_size = $_FILES['image']['size'];
$image_tmp_name= $_FILES['image']['tmp_name'];
#$desc = $_POST['desc'];
move_uploaded_file($image_tmp_name,"uploads/$image_name");
echo "<img src='uploads/$image_name' width='400' height='250'><br>";
echo "your image : ";
}
?>
<footer>
tos - faq - contact us
<span id="footerRight" class="copyright footerFont"><strong>#imgserv v1.1 </strong>
</span>
</div>
</footer>
</body>
</html>
The problems are not caused by the fact your website does not use a database.
The error message does point out that the elected upload directory cannot be found. Before attempting to write to that directory it would be better to test that it exists and if it does not create the necessary file structure. The function createdir below will recusively create the folder structure.
The upload handling code referenced a non-existant form field desc so you would likely have found an issue with that also.
<?php
function createdir( $path=null, $perm=0644 ) {
if( !file_exists( $path ) ) {
createdir( dirname( $path ) );
mkdir( $path, $perm, true );
clearstatcache();
}
}
$uploadstatus = false;
if( isset( $_POST['upload'], $_FILES['image'] ) ){
$obj=(object)$_FILES['image'];
$name=$obj->name;
$tmp=$obj->tmp_name;
$size=$obj->size;
$error=$obj->error;
$type=$obj->type;
if( $error == UPLOAD_ERR_OK && is_uploaded_file( $tmp ) ){
$target_directory = $_SERVER['DOCUMENT_ROOT'] . '/uploads';
$target_file = $target_directory . DIRECTORY_SEPARATOR . $name;
if( !file_exists( $target_directory ) ){
createdir( $target_directory );
}
$uploadstatus = move_uploaded_file( $tmp, $target_file );
}
}
?>
<html>
<head>
<title>image sharer</title>
<style>
footer {
font-family: sans-serif;
position:absolute;
bottom:0;
width:100%;
}
</style>
</head>
<body>
<h1><font color="green">#img</font><font color="blue">serv</font></h1>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label>Image selector :<input type="file" name="image"></label><br/>
<label>Description: <input type='text' name='desc' /></label><br/>
<input type="submit" name="upload" value="upload">
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && $uploadstatus==true ){
echo "
<img src='/uploads/{$name}' width='400' height='250' />
<br />
Your image : {$name} - {$_POST['desc']}";
}
?>
</form>
<footer>
tos -
faq -
contact us
<span id="footerRight" class="copyright footerFont"><strong>#imgserv v1.1 </strong></span>
</div>
</footer>
</body>
</html>
uploads folder is missing in your directory..please create uploads directory in your website folder

Wamp issue for testing upload PHP file

I'm trying to learn how to upload by using Wamp server..
But unsure is it the settings or my code.
Please enlighten me.
Upload.htmlS
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="http://localhost/testing/php/file_uploader.php" method="post"
enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
The uploader.php
<?php
if( $_FILES['file']['name'] != "" )
{
copy( $_FILES['file']['name'], "C:\wamp\www\beta\images" ) 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>
The error i've received is ,
Warning: copy(bannerbelow.JPG): failed to open stream: No such file or directory in C:\wamp\www\beta\admin\php\file_uploader.php on line 4
This works for me:
<?php
if( $_FILES['file']['name'] != "" ) {
$uploadfolder = $_SERVER['DOCUMENT_ROOT']."/beta/images";
$filename = $_FILES['file']['name'];
move_uploaded_file( $_FILES['file']['tmp_name'], "$uploadfolder/$filename" ) or
die( "Could not copy file!");
}
else {
die("No file specified!");
}
?>
$path = 'C:\wamp\www\beta\images\'; // make sure dir is exist
if(!is_dir($path)){
mkdir($path)
}
//write code for move file via copy or move_uploaded_file funcition

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.

php file upload failed

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"]);
}
?>

Categories