$_FILES['file'] does not work for a simple procedure - php

Unable to upload an image
The problem is that the image does not get set as `$_FILES['file'].
The code is very simple but still cannot get the obvious outcome.
Have marked the unexpected trouble with 3* i.e. '*'
I don't know what I am doing wrong.
Thanks in advance, your suggestions and comments are much appreciated.
Happy Coding!
<--- NOTE: HTML form below PHP --->
<?php
//CHECK: Submit
if(isset($_POST['submit']))
{
echo 'form submitted <br />';
//CHECK: File by POST
if(isset($_POST['file']))
{
echo $_POST['file'] . '<br />';
unset($_POST['file']);
}
//CHECK: File if 'isset' $_FILES
if(isset($_FILES['file']))
{
print_r($_FILES['file']);
}
else
{
echo '$_FILES not set!';
}
}
?>
<!-- HTML FORM -->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="5M">
Image: <input type="file" name="file" size="50" id="file" />
<br /><br />
<input style="margin-left:15%;" type="submit" name="submit" value="Upload"/>
</form>

Please correct your form action you missed `echo :-
<?php echo $_SERVER['PHP_SELF']; ?>
^
As your comment, your are getting error 2 that means:-
UPLOAD_ERR_FORM_SIZE
Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was
specified in the HTML form.
Please refer this solution as well File upload ['ERROR']= 2

Following correction are needed.
1 - Add an echo in action="<?php $_SERVER['PHP_SELF']; ?>" as action="<?php echo $_SERVER['PHP_SELF']; ?>"
2 - Why your are finding file in $_POST, need a check to it
if(isset($_POST['file'])){
echo $_POST['file'] . '<br />';
unset($_POST['file']);
}
3 - You have commented your out put is from line print_r($_FILES['file']);
Array (
[name] => imagename.jpg
[type] => [tmp_name] =>
[error] => 2
[size] => 0
)
tmp_name not getting blank that means your file is not creating its temporary file to upload.
So, check temp upload file path in php.ini make sure it is working

different value in name attribute
Image: <input type="file" name="fileupload" size="50" id="file" />
<?php
if(isset($_POST["submit"]))
{
$name=$_FILES['fileupload']['name'];
$name=$_FILES['fileupload']['tmp-name'];
$name=$_FILES['fileupload']['size'];
}
?>

Related

Not able to insert images into database using wpdb

I am trying to run this code to take image input from front end. I know I can't simply upload images using just this $_POST['img']. I read that I have to use file_get_contents() for this purpose. But I am new to this , I don't know How exactly to use file_get_contents()
However I tried this code, but I am unsuccessful in inserting.
<form action="" method="post">
<label id="img">image: <input type="file" name="img" id='media'/></label>
<input type="submit" name="Submit" value="upload"/>
<?php
global $wpdb;
if($_POST['Submit'])
{
$image=$_POST['img'];
if($wpdb->insert(
'image',
array(
'image' => $image
)
) == false) echo 'Database Insertion failed';
else echo 'Database insertion successful<p />';
}
?>
This did not work. After this I tried this. But still unsuccessful.
<form action="" method="post">
<label id="img">image: <input type="file" name="img" id='media'/></label>
<input type="submit" name="Submit" value="upload"/>
<?php
global $wpdb;
if($_POST['Submit'])
{
$image=$_POST['img'];
$item = file_get_contents($_FILES['img']['tmp_name']);
if($wpdb->insert(
'image',
array(
'image' => $item
)
) == false) echo 'Database Insertion failed';
else echo 'Database insertion successful<p />';
}
?>
for this I am getting a warning
Warning: file_get_contents(): Filename cannot be empty in C:\xampp\htdocs\kite\wp-content\plugins\php-code-widget\execphp.php(27) : eval()'d code on line 11
Somebody please help me out.
And in my database, in the table "image" the column "image" has the data type BLOB, not sure if the problem is with this.
Thanks in advance
For file uploads you should add, enctype="multipart/form-data"
<form enctype="multipart/form-data" action="" method="post">
^ ^
First upload to some folder and then use file_get_contents()
if (move_uploaded_file($_FILES['file']['tmp_name'], "your path". $_FILES["file"]['name'])) {
// image will get uploaded here
}
Then use
file_get_contents("new path")

PHP uploaded file name doesnt show up in browser

All of my other form data is visible, but the name of the file is not showing up in the browser.
Here is a little portion of my code:
<form method="POST" action=<?php echo $_SERVER["PHP_SELF"];?> entype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="file" value="yoyo">
</form>
<?php
echo $name = $_FILES["file"]["name"];
echo "problem";
?>
and this is the output:
Notice: Undefined index: file in D:\xamp\htdocs\colgWeb\index.php on line 228
problem
Use a validator: You misspelled enctype (it has a c in it).
Consequently, the form is being submitted with the default (url based) encoding which doesn't support file uploads.
You need to think that as a two step page. First, you send your form, then you use the input.
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="file" value="yoyo">
</form>
<?php
if (isset($_FILES["file"]))
{
$name = $_FILES["file"]["name"];
echo "File: $name";
}
?>
Please try below code. You are using same name (ie "file") for both file and submit button."
<form method="POST" action=<?php echo $_SERVER["PHP_SELF"];?> entype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit" value="yoyo">
</form>
<?php
echo $name = $_FILES["file"]["name"];
echo "problem";
?>

Submiting a form using PHP

I am trying to submit a form using PHP. I am trying to grab the value from two file inputs in my form, yet when I try to index them with my PHP code, I keep getting an error.
The error I get:
Undefined index: profile-pic in C:\xampp\htdocs\shareitme\form-test.php on line 5
Undefined index: cover-pic in C:\xampp\htdocs\shareitme\form-test.php on line 6
My Code:
<?php
if (isset($_POST['submit'])) {
$profile_pic= time() . $_FILES['profile-pic']['name'];
$cover_pic= time() . $_FILES['cover-pic']['name'];
}
?>
<form id="editprofile" method="post" action="<?php echo $_SERVER['PHP_SELF']
?>">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000"/>
<input type="file" name="profile-pic"/>
<input type="file" name="cover-pic"/>
<input type="submit" name="submit" value="submit"/>
</form>
I know I have the names right, what am I doing wrong?
For uploading a file include enctype="multipart/form-data" in your form, like below:
<form id="editprofile" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']
?>">
The enctype attribute specifies how the form-data should be encoded when submitting it to the server.
For more info on forms : http://www.w3schools.com/tags/att_form_enctype.asp
For renaming a uploaded file, it better to upload it to server with move_uploaded_file and than rename it.
try:
<?php
if (isset($_REQUEST['go']) && $_REQUEST['go'] == "1") {
$profile_pic= time() . $_FILES['profile-pic']['name'];
$cover_pic= time() . $_FILES['cover-pic']['name'];
}
?>
<form id="editprofile" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000"/>
<input type="file" name="profile-pic"/>
<input type="file" name="cover-pic"/>
<input type=hidden name="go" value="1">
<input type="submit" name="submit" value="submit"/>
</form>
Whenever you are want to upload file. you need to add enctype in from.
`enctype="multipart/form-data"'
This should work
<form id="editprofile" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']
?>">
What you're trying to do here, if I'm not mistaken, is upload images on your server, right? You can simply use a third-party upload tool for that. I'd recommend AjaxUpload as it is based on AJAX and pretty easy to implement.
For simplicity, I'm applying the upload functionality on one image. You can simply put it inside a function and reuse it.
<p id="showMsg"></p>
<input type="file" name="profile-pic" id="profilePic" />
JQuery:
$("#profilePic").ajaxUpload({
url : $_SERVER['PHP_SELF'],
name: "file",
onSubmit: function() {
$('#showMsg').html('Uploading ... ');
},
onComplete: function(result) {
$('#showMsg').html('File uploaded with result' + result);
}
});
PHP:
<?php
if (isset($_FILES))
{
$file = $_FILES['file'];
move_uploaded_file($_FILES["file"]["tmp_name"], 'upload_dir/' . $_FILES["file"]["name"]); // This function will upload the image to 'upload_dir' folder. You can modify it as per your requirements.
}
?>
1) if you are using file type then your form attribute should be enctype="multipart/form-data".
<form id="editprofile" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
2) Before using your post value it is best practice to check whether its setted or not.
$pic=isset($_FILES['profile-pic']['name']) ? $_FILES['profile-pic']['name'] : '';
Use enctype="multipart/form-data" in form tag.
Whenever you want to post binary datas you must have to set this enctype attribute.

<input type=file> not getting image

I am trying to get image file name by tag, But when I check it through isset($_FILES['imgFile']), it returns always false.
Here is my HTML tag for getting image file:
<input type="file" name="imgFile" accept="image/*" id="imgFile" src=""/>
Here is my php code to retrieve it:
if(isset($_FILES['imgFile']))
{
$img = $_FILES['imgFile']['name'];
echo $img";
}
else
{
echo "Image not set";
}
It always generate "Image not set" as an output though I have selected an image.
Are you using the correct enctype on the form?
<form enctype="multipart/form-data">
This is required when using a file upload element.
just use:
enctype="multipart/form-data"
Say this is your form:
<form action="same_page.php" method="post" enctype="multipart/form-data">
<input type="file" name="imgFile" accept="image/*" id="imgFile" src=""/>
<input type="submit" name="upload" value="Upload" />
</form>
and your php code to retrive name of the image is in the same page where the form is, then your code should be like this:
<?php
if (isset($_POST["upload"])) {
if (isset($_FILES['imgFile'])) {
$img = $_FILES['imgFile']['name'];
echo $img;
} else {
echo "Image not set";
}
}
?>
But if your php code is in another page, then you only need to use enctype="multipart/form-data" in the form as mentioned in the other answers.

How can I debug problems with move_uploaded_file?

I have a form like
<form action="send.php" method="post" enctype="multipart/form-data">
<div>
<label for="subject">Subject</label>
<input type="text" name="subject" />
</div>
<div>
<label for="image">Image</label>
<input type="file" name="image" />
</div>
<input type="submit" value="Send" />
</form>
PHP like
echo '<pre>'; print_r($_FILES); echo '</pre>';
if (move_uploaded_file($_FILES['image']['tmp_name'], 'images/' . $_FILES['image']['name'])) {
echo 'ok';
} else {
echo 'error!';
};
I keep getting error the print_r looks like
Array
(
[image] => Array
(
[name] => Untitled-1.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpprWdjN
[error] => 0
[size] => 61768
)
)
Activate error reporting, then you should see the error thrown by move_uploaded_file telling you what's wrong.
Your $_FILES looks file, error=0 means the upload completed successfully. Most likely it's a permissions error. You can try doing something like:
if (!is_writeable('images/' . $_FILES['image']['name'])) {
die("Cannot write to destination file");
}
However, be aware that you're using a user-provided filename, so if someone uploads "pwn_my_server.php", your script will write it out to the images directory, and then they can simply visit yoursite.com/images/pwn_my_server.php and take control of your site.
In general it is NEVER a good idea to trust anything in the $_FILES array, or use it directly, since the entirety of its contents are under remote user control. The only thing created by the server is the error code and tmp_name. The rest is potentially malicious.
maybe the problem is 'image/' folder, you can set the absolute path here and make sure that path is writable, then have a try.
Use the code below:
1. create the directory named 'uploads'
2. save the file with .php extension
now run the code.
<?php
if (!empty($_FILES))
{
// PATH TO THE DIRECTORY WHERE FILES UPLOADS
$file_src = 'uploads/'.$_FILES['image']['name'];
// FUNCTION TO UPLOAD THE FILE
if(move_uploaded_file($_FILES['image']['tmp_name'], $file_src)):
// SHOW THE SUCCESS MESSAGE AFTER THE MOVE - NO VISIBLE CHANGE
echo 'Your file have been uploaded sucessfuly';
else:
// SHOW ERROR MESSAGE
echo 'Error';
endif;
}
?>
<form action="" method="post" enctype="multipart/form-data">
<div>
<label for="subject">Subject</label>
<input type="text" name="subject" />
</div>
<div>
<label for="image">Image</label>
<input type="file" name="image" />
</div>
<input type="submit" value="Send" name='submit' />
</form>
:)

Categories