For the sake of clarity I'll break this up into easy to navigate and readable sections.
The Issue:
The $_FILES super-global doesn't appear to hold any information after I upload a file. Despite this, I get error code 0 (successful upload) which seems contradictory.
Background:
I've been working on a personal website for a while now, and am working on a post management system. It's been going great so far, but I'm stuck fast trying to upload an image using $_FILES.
I've done this kind of thing many times at school and my job, but there appears to be some sort of issue with my computer, or I'm having a massive stroke of idiocy.
To fix this issue I've already:
Enabled file uploads in php.ini-production for the version I am using (php 7.1.7)
Changed values such as upload_max_filesize and post_max_size to 100m
Inspected the error code PHP is returning, returns error code 0, or UPLOAD_ERR_OK
Checked my own code for any simple mistakes I'm making. I can't see any, but that's usually the case when you have an error.
I'll include a small test page I wrote, and is demonstrating the issue:
text.php:
<?php
if(isset($_POST['submit'])){
echo $_FILES['a']['error'];
if(isset($_FILE['a']['tmp_name'])){
echo "hello";
}
}
?>
<form action = "text.php" method = "POST" enctype="multipart/form-data">
<label for = "a"> Send Image </label>
<input type = "file" name = "a" id = "a">
<input type = "submit" name = "submit" value = "Submit">
</form>
when the form is submitted, I get error code 0 but the hello test statement does not trigger.
Global array is named $_FILES you are checking for isset($_FILE['a']['tmp_name']
And if you want to check for uploaded file try using
if (is_uploaded_file($_FILES['a']['tmp_name'])) {
//file is uploaded
}
Related
I am using a simple file operation on the PHP in order to edit the config file for network interface on CentOS 6.7(/etc/sysconfig/network-scripts/ifcfg-eth0
), after change in any value and save into the config file and try to restart the network interface i get this error:
does not seem to be present, delaying initialization.
[FAILED]
my PHP code is this:
<?php
// configuration
$file = '/etc/sysconfig/network-scripts/ifcfg-eth0';
// check if form has been submitted
if (isset($_POST['text']))
{
// save the text contents
file_put_contents($file, $_POST['text']);
// redirect to form again
header('Location: network.php');
exit();
}
// read the textfile
$text = file_get_contents($file);
?>
<!-- HTML form -->
<form action="" method="post">
<textarea style="width:50%; height:50%;" name="text"><?php echo htmlspecialchars($text) ?></textarea>
<input type="submit" />
<input type="reset" />
</form>
i need manually called the network script by command setup and do a modification in the device setting and save then i will be able to restart the network interface. appreciate if anyone help me why this issue happen while if i open the config file and edit it manually it wouldn't cause this issue.
It's most likely that the user name the server runs as (by default apache on most Red Hat-based distributions) doesn't have permission to write to/etc/sysconfig/network-scripts/ifcfg-eth0.
You should check the return values of:
file_put_contents($file, $_POST['text']);
From http://php.net/manual/en/function.file-put-contents.php
This function returns the number of bytes that were written to the file, or FALSE on failure.
I presume this script will only be run by your or trusted colleagues given that there's no validation of the user input. Indentation would also make the PHP code more readable.
I have the following code that attempts to take a users form input of a file, and upload it to the webserver.
This code does work on a Apache server, however I'm now trying to get the same code working on my Windows IIS 6 web server, which has PHP (Version 5.2.3) installed and working. I have set the PHP.INI file so that
file_uploads = On
upload_tmp_dir = "C:\Temp"
My form is
<form method="POST" action="do_upload.php" enctype="multipart/form-data">
<input type="file" name="img1" size="30">
<input type="submit" name="BtnUpload" value="Click To Upload Now">
</form>
My PHP code to do the upload is
$abpath = "C:\MyWebs\Website1\httdocs\images";
#copy($img1, "$abpath/$img1_name") or $log .= "Couldn't copy image 1 to server";
if (file_exists("$abpath/$img1_name"))
{
$log .= "File 1 was uploaded";
}
else
{
$log .= "File 1 is not an image";
}
For some reason when I check the value of $img1 e.g echo $img1; it is empty. Therefore I tried to get the file using $_FILES['img1']['name']. This worked fine, but still I couldn't upload any files
Any ideas why this is happening.
Your code should be:
move_uploaded_file($_FILES['img1']['tmp_name'], "$abpath/$img1_name");
Don't copy() uploaded files. There are a few edge cases where an uploaded file can be tampered with, which is why move_uploaded_file() exists - it checks for those particular types of tampering.
As well, be VERY careful with how you create your filenames when processing the upload. If you directly use ANYTHING provided in $_FILES as part of the destination path/name for the file, you are opening bad security holes on your server, and a malicious user can exploit that to scribble a file anywhere they want on your server.
I have a form with the possibility to upload an image from the computer to a server, but it won't work. I don't get any error message, so that's quite annoying. (First I got permission denied, but that was solved by changing the rights), but now when I submit the form, everything goes normally, but the file isn't copied to the destination folder. (The folder exists: I tried it with file_exist()...)
Here's part of the code:
<form action='/changingfruit/index.php?item=bad' name='form' method='post' enctype='multipart/form-data'>
<tr>
<td><input type='text' name='titel_nl' value="titel nl" /><br/><input type='text' name='titel_fr' value="titel fr"/></td>
<td><input type='file' name='text_nl' id='text_nl' accept="image/*"/><br/><input type='file' name='text_fr' id="test_fr" accept="image/*"/></td>
<td class="vTop"><input type="submit" value="Bewaar"/></td>
</tr>
</form>
Part where the values are being send to the db:
$str_titel_nl = $_POST["titel_nl"];
$str_titel_fr = $_POST["titel_fr"];
$str_text_nl = $_FILES["text_nl"]["name"];
$str_text_fr = $_FILES["text_fr"]["name"];
if(!empty($_FILES["text_nl"]["name"])){
$tmp = $_FILES['text_nl']['tmp_name'] ;
$foto = $_FILES['text_nl']['name'] ;
$copied = copy($tmp, $images_nl.$foto);
unlink($tmp);
}
(of course the above is just a part of the code: but it's this part that wont work:
if(!empty($_FILES["text_nl"]["name"])){
$tmp = $_FILES['text_nl']['tmp_name'] ;
$foto = $_FILES['text_nl']['name'] ;
$copied = copy($tmp, $images_nl.$foto);
unlink($tmp);
}
The code below this part also works fine, so no error, but also no image.
Does someone knows where the problem could be?
Thanks so much in advance!
FOUND THE ANSWER
So it was indeed a permission problem. Everything was 777, but the last folder where the image was put had 755. (/fruits/img/2012/thumb/) the thumb was 755.I just overlooked it. Thanks everyone for the help!
Your upload code is very messy. Instead of using copy you should be using move_uploaded_file, and also validate that it actually worked and then perform whatever actions needed.
I'm also not sure why each of your line is starts with <?php and ends with ?> ?
You can write it all as one block instead, and i think it would also make more sense and would make your code cleaner for sure.
Last thing i would recommend is reading "Handling File Uploads" from the PHP Manual. It might shed some light on the problems you're having.
P.S. Try adding on top ini_set("display_errors","On"); error_reporting(E_ALL); and see if you're getting any error messages.
please have a look on below link.
PHP upload file to web server from form. error message
http://patelmilap.wordpress.com/2012/01/30/php-file-upload/
you can try this
$flag = #copy($temp, $move);
if ( $flag === true )
{
print "Uploaded";
}
I have posted a simple solution for file uploading without worrying about the implementation .
Click to see the thread
image uploading issue in codeigniter 2.1.0
Please read this section
in that $uploader->getMessage(); will return error string related to the upload failure . So you can understand why the uploading failed .
Thanks
So I want to:
upload a csv file which will contain a list of student numbers, one to each line (392232, per line).
populate an array with the student numbers (as i already have a process in place of looking up ids from an array of student numbers and storing etc if they were to add students manually)
I have been lookin at a tutorial found here.
however I am slightly confused with this:
if(isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])){...
where does he establish 'tmp_name' from?
anyway, if somebody could explain how I should be going about this I would appreciate the help.
many thanks,
EDIT: added progress of where it is not working.
if(isset($_POST['csv_submit'])){
if(isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])){
//upload directory
$upload_dir = "/ece70141/csv_files/";
//create file name
$file_path = $upload_dir . $_FILES['csv_file']['name'];
//move uploaded file to upload dir
// GETTING THE ERROR BELOW.
if (!move_uploaded_file($_FILES['csv_file']['tmp_name'], $file_path)) {
//error moving upload file
echo "Error moving file upload";
}
print_r($_FILES['csv_file']);
//delete csv file
unlink($file_path);
}
}
$_FILES is a magic superglobal similar to $_POST. It's an array of every file that's been uploaded in the last request, and where that file is stored (tmp_name).
tmp_name is basically generated by the web server to let PHP know where they've stored the file.
You have the following items available to you in each element of the $_FILES array:
name (Original Name of the file)
type (MIME Type of the file, ie. text/plain)
tmp_name (Path to the uploaded files' temporary location)
error (Error occurred when uploading, 0 when no error)
size (Size of the uploaded file, in bytes)
From what I can see in your code, this will work perfectly fine and as discussed in comments, I think the issue lies in your HTML.
The tutorial that you linked to has an incorrect <form ..> tag definition. For file uploads, you're required to set the enctype attribute, below is what it should look like:
<form action="" method="post" enctype="multipart/form-data">
I've been all over the internet reading up on APC, and it seems like a nifty way to detect file Uploading.
I am, however, having a problem.
I know how to call files and everything using Ajax, and that is what I am planning to do, but for Testing sake, I'm doing something like this.
Ok, so I have 3 files.
form.php
upload.php
status.php
form.php contains:
<input type="hidden" name="APC_UPLOAD_PROGRESS" value="1234" />
<input type="file" id="fileIn" name="file" />
(I am aware that I will need to use a unique ID in APC_UPLOAD_PROGRESS. Again, this is just for testings sake.)
Ok, Now Upload.php has the regular PHP upload script:
$origin = $_FILES['file']['name'];
if(move_uploaded_file(...etc...etc)...
And Status.php uses APC:
$upload = apc_fetch('upload_1234');
if ($upload) {
if ($upload['done'])
$percent = 100;
else if ($upload['total'] == 0)
$percent = 0;
else
$percent = $upload['current'] / $upload['total'] * 100;
echo $percent;
}
Now What I am doing is uploading a file using a regular HTTP method, and using another window to monitor Status.php.
The problem is; Status.php returns nothing!
However, If i write
print_r(apc_fetch('upload_1234'));
into upload.php, it returns the correct array, with all the details etc..
What am I doing wrong?
Thanks.
When this happens, something to check is that your hidden input element with the APC_UPLOAD_PROGRESS key is placed immediately before the file input in your form.
I know the form in the example above does do this, but it's easily missed in a more complicated form layout.