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.
Related
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';
I'm trying to add an upload feature in an existing Wordpress plugin. But the file seems not to load. I use this code in the form:
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
// other code of existing plugin.
<input type="submit" value="plugin submit" id="plugin submit" name="plugin submit">
</form>
Then in the function of the plugin I put the test line for knowing what happens:
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"];
}
I enter in else branch but all fields are empty. What am I missing?
You will hit the else branch even if $_FILES is undefined.
You can first check that the user uploaded a file.
is_uploaded_file()
Returns TRUE if the file named by filename was uploaded via HTTP POST.
You can use somthing like:
if(is_uploaded_file($_FILES['file']['tmp_name'])
But since it seems this will not return TRUE in your case I must ask - since your form is POSTing to the same page that it is on - is your PHP code at the top of the page where your form resides? If that is the case don't you find it odd that it outputs the blank upload information even before you upload?
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>
I'm using WordPress and I'm following W3's guide for uploading a file:
HTML code:
<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>
PHP code (upload_file.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"];
}
?>
The HTML code is pasted in a PHP page template and the PHP file under the WP installation directory under www.
The problem is when I submit the file I get Error: 1.
If I remark the "if" part of the PHP code and leave the "else" part I get:
Upload: IMG_4258.JPG
Type:
Size: 0 Kb
Stored in:
So at least I know the PHP code is running.
But what's causing it to fail?
Is there a problem with the code or is WordPress meddling with the process?
If you go to the PHP manual page, instead of W3schools (sometimes not really a good resources), you would see that your error code 1 is:
UPLOAD_ERR_INI_SIZE
Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
That means your INI directive restricts your file upload (by default it should be 2Mbyte)
I know your is just a stub, but I suggest you doing chekcs on the kind of uploaded files, but especially actually DO UPLOAD the file (not just having it stored and then destroyed in the temporary directory) by using move_uploaded_file()
Your image file is too big according to your error code.
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