File Upload not Working in PHP - php

I am new to PHP and am trying to create a page to upload a jpeg file. The webpage seems to run fine and it appears the file is uploading, however the file is not appearing on the server. Any help you can provide will be great.
The PHP code is:
<?php
$target_dir="/var/www/html/";
$fileName=$_FILES['file']['name'];
$target_file=$target_dir . basename($fileName);
$imageFileType=pathinfo($target_file,PATHINFO_EXTENSION);
$fileTempName=$_FILES["file"]["tmp_name"];
$fileType=$_FILES["file"]["type"];
$fileSize=$_FILES["file"]["size"];
$fileError=$_FILES["file"]["error"];
if(($fileType=="image/jpeg")&&($fileSize<100000)){
if($fileError>0){
echo "Return Code: " . $fileError . "<br />";
}
else{
echo "Upload: " .$fileName . "<br />";
echo "Type: " . $fileType . "<br />";
echo "Size: " . ($fileSize / 1024) . " kb<br />";
echo "Temp file: " . $fileTempName . "<br />";
if (file_exists($fileName)){
unlink($fileName);
}
move_uploaded_file($fileTempName,$target_file);
echo "<br><br>File Temp Name: " .$fileTempName."\r\n <br>";
echo "Uploaded file stored as : " .$target_file ."<br><br>";
}
}
else{
echo "File is not a JPEG or too big.";
}
?>
And the HTML code is as follows:
<html>
<body>
<form action="save2web.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"/>
<br/>
<input type="submit" name="submit" value="Upload"/>
</form>
</body>
</html>

The problem is that, you don't check the returned value of move_uploaded_file() at all. But looks like, you're running your script on the host, and since most host disallow relative paths, you'd better provide an absolute.
So try, replacing:
$target_dir = "/var/www/html/"; // <- This is relative, which might be blocked due to security reasons
with
$target_dir = dirname(__FILE__) . "/var/www/html/"; // dirname(__FILE__) is a path to root
And then, make sure that the file was uploaded:
if (!move_uploaded_file(...)) {
// error
}

Presuming your server is linux from the path, you may need to change the permissions of the folder your trying to upload to, try changing the folder owner to www-data, this fixed the same issue for me.

Change your target directory like this. Add a .(dot) before the directory name.
$target_directory = './var/www/html';

Related

Cannot copy or move uploaded files in Windows Server

(The question was initially asked in Server Fault. I move it to here, because I don't have enough credit to start a bounty over there.)
I am trying to make my Windows Server 2012 R2 to host a webpage where users could upload a file.
<!-- indexfile.html -->
<form action="uploadfile.php" method="post" enctype="multipart/form-data">
Browse for File to Upload: <br>
<input type="file" name="file" id="file" size="80"> <br>
<input type="submit" id="u_button" name="u_button" value="Upload the file">
</form>
// uploadfile.php
<?php
$file_result = "";
if ($_FILES["file"]["error"] > 0)
{
$file_result .= "No File Uploaded or Invalid File ";
$file_result .= "Error Code: " . $_FILES["file"]["error"] . "<br>";
} else {
$file_result .=
"Upload: " . $_FILES["file"]["name"] . "<br>" .
"Type: " . $_FILES["file"]["type"] . "<br>" .
"Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br>" .
"Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (chmod("C:/inetpub/testaddbbacom/test", 0777))
$file_result .= "chmod sucessful!<br>";
else
$file_result .= "chomod NOT sucessful!<br>";
copy($_FILES["file"]["tmp_name"], "C:/inetpub/testaddbbacom/test/" . $_FILES["file"]["tmp_name"]);
$file_result .= "File Upload Successful!";
echo $file_result;
}
?>
The problem is, files can be uploaded into C:\Windows\Temp\, whereas copy does not work. As consequence, C:\inetpub\testaddbbacom\test is always empty. I did try to change the permission of this folder by chmod, but it does not really work. Here is a comparaison of the permissions of two folders:
I also tried move_uploaded_file, but it did not work either, probably due to the same permission reason...
Does anyone know how to solve this problem?
Replace,
copy($_FILES["file"]["tmp_name"], "C:/inetpub/testaddbbacom/test/" . $_FILES["file"]["tmp_name"]);
With the following,
move_uploaded_file($_FILES["file"]["tmp_name"], "C:/inetpub/testaddbbacom/test/" . $_FILES["file"]["name"]);
the difference is use file name i.e $_FILES["file"]["name"]) and not $_FILES["file"]["tmp_name"]) in destination path.

Basic PHP image upload not working

I'm trying to debug an issue with a WordPress 3.5.1 where I cannot upload media via HTTP at all; the media uploader simply says "HTTP error" and fails. To diagnose what's going on, I decided to write (i.e. copy from w3schools) a really basic PHP file uploader to see if there's something weird going on behind the scenes. But for some reason, the $_FILE structure doesn't contain any information at all, even in the most basic of examples:
file.php :
<html>
<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="submit" value="Submit">
</form>
</body>
</html>
upload.php :
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
In every browser I've tried, with every file I've tried, this just outputs:
Upload:
Type:
Size: 0 kB
Stored in:
and printing $_FILES shows that it is just an empty array.
I'm using PHP-5 on shared hosting (lunarpages), but the php.ini file has file_uploads on and the size of the files I tried is nowhere even close to the upload_max_filesize. I am ready to throw my laptop against a wall, so any help would save me a couple thousand dollars.
<html>
<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="submit" value="Submit">
</form>
<?php
phpinfo();
?>
</body>
</html>
This worked for me, $_FILES was not empty, using a common Debian installation, PHP 5.2; try to adjust the permissions of your files and direcotires to 777
I had a similar issue once with a shared host. Turned out that I had no tmp directory set, therefore nothing was being uploaded. Check with your host that your php.ini is set up correctly and that a tmp directory exists for uploads and that you have write permissions for it.
$target="Your Path";
if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
else
{
echo "Unable to move temp file to target.";
}
If it is not yet too late, you should check your apache logs. The most probable cause is that the apache - and so your php interpreter - has no permission to your predefined upload directory.

Upload image file using href in move_uploaded_file( ) php

I need some advice and guide line.
I am trying to upload image into a folder using link. And I am unable to upload an image.
I am trying this code. This shows error message :
[function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if (file_exists("http://localhost.myimage.com/uploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"http://localhost.myimage.com/uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
?>
<html>
<body>
<form action="" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Is it possible ?
The destination of the moved file needs to be an absolute path to the server, i.e.
move_uploaded_file( $_FILES["file"]["tmp_name"], $_SERVER['DOCUMENT_ROOT'] ."/uploads/" . $_FILES["file"]["name"]);
You are supplying file_exists and move_uploaded_file with an URL. Try supplying a file on the hard drive instead. Change:
file_exists("http://localhost.myimage.com/uploads/" . $_FILES["file"]["name"])
to
file_exists($_SERVER['DOCUMENT_ROOT'] . "uploads/" . $_FILES["file"]["name"])
and similar to move_uploaded_file since that's where the error originated.
No it's not possible this way.
You try to store the uploaded file on a wrong destination.
I assume you own "http://localhost.myimage.com/uploads/" and wan't to store the file under '/uploads/' in your server.
If your project is installed e.g. under /var/www/my_prj/
And your upload is under /var/www/my_prj/uploads
then your code should look like:
move_uploaded_file($_FILES["file"]["tmp_name"],
"/uploads/" . $_FILES["file"]["name"]);
This only applies when '/var/www/my_prj/' is the DocumentRoot for your apache configured host.

Upload logo on link click

I'm new to the concept of file uploading using a form in PHP. I would like to know a PHP script for the purpose of uploading a logo image onto a site. I tried couple of sample demos available on the web but I didn't get the image into my folder.
PS: I didn't understand many of the scripts given.
I would like to upload a logo image to replace my existing logo. How to do that?
I have a link 'Change Logo' on clicking which it should go to another page where I can use a file uploading form. {I would prefer if the form didn't have an action.} But where will the saved image be posted. I would like a customized script that would upload an image into my folder 'style/images/' as 'logo.png'.
Check out the following link, it will show a step by step procedure of file upload through the clear comments in the script itself. Hope this helps
http://www.reconn.us/content/view/30/51/
Step 1 - Copy paste the above script and save it in a folder
Step 2 - Create a folder with name images in the same folder where the .php file with the above script pasted.
Step 3 - Upload a file sizes not greater than 100kb. The output should be File Uploaded Successfully! Try again!
Step 4 - To check whether the file is uploaded, check the images folder, you will find the uploaded file in the folder.
OR Try this
<?
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
// echo "Upload: " . $_FILES["file"]["name"] . "<br />";
// echo "Type: " . $_FILES["file"]["type"] . "<br />";
// echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
// echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("images/" . $_FILES["file"]["name"]))
{
$_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"images/" . $_FILES["file"]["name"]);
echo "Stored in: " . "images/" . $_FILES["file"]["name"];
}
}
?>
<html>
<body>
<form action="index.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" class="validate[required] text-input" />
<input type="submit" name="submit" value="Attach file" >
</form>
<form id="form1" name="form1" action="">
<input type="hidden" id="filename" value="<?echo $_FILES["file"]["name"];?>"/>
</form>
</body>
</html>

Can't find uploaded files in temp directory after script execution ends

I am trying out a tutorial on W3 schools to learn how to create forms for PHP uploads.
To this end, I have the following two files as shown on W3 schools:
The HTML file:
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
and the corresponding PHP file as follows:
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
When I save the above files onto my localhost, and execute the up.html file, the PHP produces an output as follows:
Upload: AddTrustExternalCARoot.crt
Type: application/x-x509-ca-cert
Size: 1.4853515625 Kb
Stored in: /tmp/phpK0YqyL
Unfortunately, I cannot seem to find this /tmp/phpK0YqyL.
Can anyone suggest where this file could possibly be located?
In reality, I would also like to know how one could specify the path to directly upload the file to (presumably this would be somewhere in $_FILES array).
The /tmp/ folder is a folder to temporarily store the file for processing or reading.
If you want to access the file later on, you need to save the file to the server with the move_uploaded_file function
Temporary uploaded files are removed when the script execution ends.
remember to set /temp/folder for read write files

Categories