How can I debug problems with move_uploaded_file? - php

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>
:)

Related

php form works fine on localhost, but send empty inputs online

I have some forms in php that upload data into a mysql database. I have already tested on a localhost server with xampp, but when I upload my files into my host ftp, the forms do not work. I think it has something to do with the fact that I put this file and also the "connect.php" file under password restriction, and that has something to do with the file permissions. I've already granted permission to the user of the database and permission to the file and I also had tried without the password protection, but the result is the same.
So I start to experiment a little and I figured out that the image uploads fine into the database but the other inputs don't, neither the "header" input nor the "article" textarea... knowing that, I tried to "echo out" these input results but none of them show something unless the image input. I came to the conclusion that my form is sending empty data. Can someone make suggestions as to what is happening and how to fix it? Here's my code:
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<div id="slider_upload">
<h1>Slider principal</h1>
<div class="forma">
<form action="sliderPrincipal.php" method="post" enctype="multipart/form-data">
<p><label for="header">Header</label>
<input type="text" id="header" name="header" /></p><br>
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
<p><label for="fileupload">File to upload</label>
<input type="file" id="fileupload" name="fileupload" /></p><br>
<p><label for="article">article</label>
<textarea id="article" name="article" rows="26" style="width: 100%;" >Write here</textarea></p><br>
<button type="submit" name="submit" value"send">Upload File</button>
</form><br><br>
</div>
</div>
<div class="message">
<?php
$file_dir = "../uploaded";
if (isset($_POST['header'])){
$header = mysql_real_escape_string($_POST['header']);
$article = mysql_real_escape_string($_POST['article']);
}
foreach($_FILES as $file_name => $file_array){
//echo "path: ".$file_array['tmp_name']."<br />\n";
//echo "name: ".$file_array['name']."<br/>\n";
//echo "type: ".$file_array['type']."<br/>\n";
//echo "size: ".$file_array['size']."<br/><br/>\n";
//echo "encabezado ".$encabezado;
if(is_uploaded_file($file_array['tmp_name'])){
move_uploaded_file($file_array['tmp_name'], "$file_dir/".$file_array['name']) or die ("Your image couldnt be uploaded <br>");
$newpath = addslashes("uploaded/".$file_array['name']);
include "connect.php";
$addfile = "INSERT INTO slider (header, image, article) VALUES ('$header','$newpath', '$article')";
$result = mysql_query($addfile);
if ( $result === FALSE ) {
echo "your post could not be uploaded but we already got your image in our files ";
} else{
echo '<p style="padding: 20px;"><h1>Your post was successfully uploaded <h2></p><br><br>';
}
mysql_close();
} else "No file found";
}
?>
</div>
</div>
</body>
</html>

Problems with $_FILES in PHP

I am trying to make a site where I can upload pictures.
<form action="edit.php" method="get" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="submit">
</form>
In the edit.php-file I have chosen to use the example from W3Schools (http://www.w3schools.com/php/php_file_upload.asp). When I get to the editor-page it won't upload because the file is a invalid type (filetype not found in the array).
After many different tries I putted this code at the top of the file:
if (!isset($_FILES['file'])) { die("Not found!"); }
When I loaded the editor page again I got the error-message I had put there myself. It seems like the file I am sending from the index.php-page won't be founded in edit.php.
Can anyone help me?
It should be like this and then in your edit.php file you can try to print the $_FILES array.
//edit.php
print '<pre>';
print_r($_FILEs);
// index.php
<html>
<body>
<form action="edit.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>
if(!empty($_FILES['file']['size'])):
$file = $_FILES['file']['tmp_name'];
//play with the file then
endif;
Change this line
<form action="edit.php" method="get" enctype="multipart/form-data">
to
<form action="edit.php" method="post" enctype="multipart/form-data">
See here for discussion on this topic.
File uploading using GET Method
Use post method.
index.php code :
<form enctype="multipart/form-data" action="edit.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
edit.php (Just printed Files array and output is as follows)
Code:
";
print_r($_FILES);
print "";
?>
Output :
Array
(
[uploadedfile] => Array
(
[name] => ipmsgclip_r_1398851755_0.png
[type] => image/png
[tmp_name] => /tmp/phpktZwLl
[error] => 0
[size] => 155
)
)
If file uploading has any error then it will come in this array as [error].
For instance : I your uploaded file's size is exceeds the mentioned file size (1000000)
Then output Array will be :
Array
(
[uploadedfile] => Array
(
[name] => d.png
[type] =>
[tmp_name] =>
[error] => 2
[size] => 0
)
)

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

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'];
}
?>

upload image file to server using php

I am trying to upload selected image file into my ftp server on 1and1. I am not able to upload the file.
I have created folder called "uploadimages".
I have created a html form which has the following:
<form action="add.php" method="POST" enctype="multipart/form-data">
<div class="logo">
<label for="logoname" class="styled">Upload Logo (jpg / png):</label>
<div class="logofield">
<input type="file" id="logo" name="logo" size="30"/>
</div>
</div>
<input type="submit" value="Upload" name="submit" id="submit"/>
</form>
I have also create a php file which has the following:
<?php
$imagepic = $_FILES["logo"]["name"];
echo $imagepic;
$tempimgloc = $_FILES["logo"]["tmp_name"];
echo $tempimgloc;
$errorimg = $_FILES["logo"]["error"];
echo $errorimg;
if($errorimg > 0)
{
echo "<strong> <font size='18'>There was a problem uploading your Logo. Please try again!</font></strong>";
echo "<BR>";
}
else
{
move_uploaded_file($tempimgloc, "uploadimages/".$imagepic);
}
?>
ECHO printed results are:
1. Filename = testimage.png
2. Temp directory = /tmp/phpHvewUP
3. Error = 0
But they are not getting uploaded..
Where could I go wrong here?
Let me know!
You need to give access to your folder. Try this, chmod 777 uploadimages.
chmod 777 uploadimages
WRITING PERMISSION was the issue. Thanks Shapeshifter!

how can i automatically create thumbnail images from an uploaded file With GD library

how can i create automatically tumbling .. I need when i upload original image automatically create thumb and add thumb ( size 200 x 200 ) in folder and insert path of thumb in database $thumb .. i need help .. any one can help me ?
<?php
error_reporting(0);
if ($_POST['submit'])
{
$name=basename($_FILES['file_upload']['name']);
$t_name=$_FILES['file_upload']['tmp_name'];
$dir='upload';
$image_title=$_POST['image_title'];
$image_details=$_POST['image_details'];
$image_text=$_POST['image_text'];
$cat=$_POST['cat'];
$thumb=$_POST['thumb'];
if(move_uploaded_file($t_name,$dir."/".$name))
{
mysql_select_db ($db_name,$conn);
$qur="insert into images (mid, cid, name, path, image_title, image_details, image_text, thumb, addGby, addGon)
values('','$cat','$name','upload/$name','$image_title','$image_details','$image_text','$thumb','$session_name',now())";
$res=mysql_query($qur,$conn);
echo 'File Upload sussfully';
}
else
{
echo 'upload filed!';
}
}
?>
<form method="post" action="Addgallery.php" enctype="multipart/form-data">
<div class="field">
<label>image : </label>
<input type="file" class="custom-file-input" name="file_upload" id="file_upload" />
</div>
<div class="field fullwidth last">
<input type="submit" name="submit" value="upload" class="bt blue large" />
</div>
</form>
If you dont want to write your own code try these
http://phpthumb.sourceforge.net/
http://davidwalsh.name/create-image-thumbnail-php
http://webcheatsheet.com/php/create_thumbnail_images.php
You should try imgBrowz0r http://freecode.com/projects/imgbrowz0r
It is a php-written plugin
Css-tricks also have a tutorial on it.

Categories