$_FILES remains empty - php

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?

Related

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.

PHP Upload CSV file Extra Carraige Return

I am trying to use PHP to upload a CSV file (type:application/vnd.ms-excel) to my Unix box. And then FTP it to Mainframes.
The problem occurs in uploading when extra blank lines are added to the uploaded file at the end of each line, i can see this when i download the uploaded CSV file using ftp Command in cmd prompt.
No Extra lines appear in the CSV while viewing in Unix box.
When it FTPs it to mainframe i can see a dot added to each line. Hex Dec x'0D'
Could you please help me out in truncating this extra character while uploading?
My 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>
My PHP Code "upload_file.php"
<?php
$allowedExts = array("csv", "CSV");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["type"] == "application/vnd.ms-excel")
&& in_array($extension, $allowedExts))
{
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 />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
$conn = ftp_connect("a.b.c.com") or die("Could not connect");
ftp_login($conn,"login","pass");
echo ftp_put($conn,"'dataset.name'","upload/" . $_FILES["file"]["name"],FTP_ASCII);
ftp_close($conn);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
You might want to take a look at the http://at2.php.net/manual/en/function.trim.php method.
You can open the file after storing it, iterating over each line and trim() them. You can print them back to the file (or just load everything, iterante and do a file_put_contents()). This should remove any whitespace from the beginning and the end before you upload it.
If you just want to remove it from the end, you can also use rtrim() or if you want to be more flexible you can do it with preg_replace() and something like /\s*$/.

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>

Uploading a file fails under WordPress

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.

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