Cant get the path of uploaded image in php - php

I am trying to retrieve the path of an image that I upload in the html page using php, but when i use the method $_FILES['file1']['tmp_name'] it displays an error saying "undifined index: file1".
This is the code I am trying:
HTML:
<form name="form1" method="GET" action="image.php">
<br/>
<input type="file" name="file1"/>
<input type="submit" value="submit"/>
</form>
PHP:
<?php
$files=$_FILES['file1']['tmp_name'];
$folder="images";
if(is_uploaded_file($_FILES['file1']['tmp_name']))
{
move_uploaded_file($_FILES['file1']['tmp_name'], $folder . '/'.$_FILES['file1']['name']);
$image=$folder . '/'.$_FILES['file1']['name'];
echo "Successfully Uploaded";
echo $image;
}
else
die("Not Uploaded");
?>

Uploading files require a POST method and not a GET.
Consult:
http://php.net/manual/en/features.file-upload.post-method.php
Also make sure the folder you are wanting to upload to, has proper permissions to write to it.
Sidenote edit: Plus, as stated in the other answer, a valid enctype is required.
I'd also use:
if ($_SERVER['REQUEST_METHOD'] == 'POST'){...}
In order to ensure of a POST method, while using full bracing for your else and adding another else{...} for the above.
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$files=$_FILES['file1']['tmp_name'];
$folder="images";
if(is_uploaded_file($_FILES['file1']['tmp_name']))
{
move_uploaded_file($_FILES['file1']['tmp_name'], $folder . '/'.$_FILES['file1']['name']);
$image=$folder . '/'.$_FILES['file1']['name'];
echo "Successfully Uploaded";
echo $image;
} else {
die("Not Uploaded");
}
} else{
echo "A POST method was not used here.";
}
Also a conditional !empty() against your file input.
http://php.net/manual/en/function.empty.php

Change:
<form name="form1" method="GET" action="image.php">
to
<form name="form1" method="POST" enctype="multipart/form-data" action="image.php">

Related

How to check if $_POST[image] is set

Re asking how to check if $_POST[FILE] isset
I have a file input and if I submit my form without an image I want something to happen if I uploaded a file in the input I want something different to happen.
if (!isset($_POST[image])) { }
seems to trigger regardless of whether or not I have uploaded a file in the input or not.
<label>
<p>Profile Picture:</p>
<input type="file" name="image" value="" />
</label>
My last question was marked as a duplicate of this answer Check whether file is uploaded however
if (!file_exists($_FILE['image'])) { }
didn't work either it is still showing truthy even when an image is uploaded. So not the answer I need.
To check if there is a file uploaded is you need to check the size of the file.
Then to check if its an image or not is you need to use the getimagesize() function. See my script below:
HTML:
<form action="index.php?act=s" method="post" enctype="multipart/form-data">
<input type="file" name="image" value=""/>
<input type="submit">
</form>
PHP:
<?php
if(isset($_GET['act'])){
// Check if there is a file uploaded
if($_FILES["image"]["size"]>0){
echo "There is a file uploaded<br>";
// Check if its an image
$check_if_image = getimagesize($_FILES["image"]["tmp_name"]);
if($check_if_image !== false) {
echo "Image = " . $check_if_image["mime"] . ".";
} else {
echo "Not an image";
}
}
else{
echo "There is NO file uploaded<br>";
}
}
?>

When uploading a file is_uploaded_file returns false?

im using a simple input file type to upload a pdf to the server:
<form action="subirCircular.php" method="post" enctype="multipart/form-data">
<input type="file" name="userfile" accept="application/pdf">
<br><br>
<button type="submit" class="btn btn-default">Subir</button>
</form>
And I receive the file in the php for uploading:
<?php
define ("FILEREPOSITORY","./uploads/");
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
if ($_FILES['userfile']['type'] != "application/pdf") {
echo "<p>Class notes must be uploaded in PDF format.</p>";
} else {
$name = $_POST['name'];
$result = move_uploaded_file($_FILES['userfile']['tmp_name'], FILEREPOSITORY."/$name.pdf");
if ($result == 1) echo "<p>File successfully uploaded.</p>";
else echo "<p>There was a problem uploading the file.</p>";
} #endIF
}else{
echo 'ERROR!';
}
?>
The thing is the condition never gets called, I always get a false 'is_uploaded_file'.
I would like to know what Im doing wrong, thanks!
I rather use :
$_FILES['userfile']['error']
to check if every thing is ok, and if yes then I use
move_uploaded_file($_FILES['userfile']['tmp_name'],$pathname)
to move the uploaded file.
And so far it works.
Check your request method - it should be POST, not PUT/PATCH/...

php uploaded file not visible

After reading the w3schools file (http://www.w3schools.com/php/php_file_upload.asp) I followed the same steps to upload the image file. However i'm not able to see the file in wampp\tmp folder. Here is my code:
<?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"]."<br>";
echo "stored in" .$_FILES["file"]["localhost/tom/upload/"]. "<br>";
}
?> <!DOCTYPE html>
<head>
<title>Uploading file</title>
</head>
<body>
<form name="f1" action="fup2.php" method="post" enctype="multipart/form-data">
<input type="file" name="f1" /><br />
<input type="submit" value="send" />
</form>
</body>
</html>
Only sessions are saved in the tmp folder, but not the image..
Please suggest.
You will have to move the file from the temp folder to an real one.
Try:
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
If you like following w3schools they explain it right under where you linked.
("Saving the Uploaded File")
Make sure the form has at least the following:
<form method="post" enctype="multipart/form-data">
Files can only be sent through POST, and the enctype needs to be multipart/form-data.
Check the PHP error log if that wasn't the issue.
You can find the location where temporary files are supposed to be saved with sys_get_temp_dir().

PHP Image Upload, with preview on select of a file

Ok so im making an image uploader with a chance to see what you are going to upload, before entering it into the database. My mind is kinda tired, could you please just get me through this one? thx (btw it doesnt work :p) Its on a clean page, i choose a file from my pictures on compouter, it doesn echo "hi wazzup
<?php
if(isset($_FILES['image'])){
echo "hi wazzup!";
}
?>
<form name="form" id="form" method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input type="file" name="image" onChange="if(event.propertyName=='value') {document.getElementById("form").submit();}">
</form>
See what is the output and you will know wheater your javascript is wrong or you should use $_FILES instead of $_POST for image
<?php
if(isset($_POST))
{
if(isset($_FILES['image'])){
echo "<span style=\"z-index:1000;display:block;\">hi wazzup!</span>";
}
echo "POSTED";
}
?>

Trouble with uploading a file

I can't get any files to upload successfully, it's just going to echo 'error';
HTML:
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value = "2000000">
Upload this file: <input name ="userfile" type="file">
<input type="submit" value="Send File">
</form>
PHP:
<?php
if ($_FILES['userfile']['error']>0)
{
echo 'Problem.';
exit;
}
$upfile='/uploads/'.$_FILES['userfile']['name'];
if (is_uploaded_file($_FILES['userfile']['name']))
{
if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))
{
echo 'Problem: could not move file';
exit;
}
}
else
{
echo 'Error';
exit;
}
echo 'File uploaded successfully.';
?>
I'm sure it's something simple I'm messing up, but I've spent about an hour trying to find it. Thanks.
if (is_uploaded_file($_FILES['userfile']['tmp_name']))
tmp_name instead of name
$_FILES['userfile']['tmp_name'] is the name of the uploaded file. $_FILES['userfile']['name'] is just the name that the file had when it was on the computer of the user.
For proper working, the function is_uploaded_file() needs an argument like $_FILES['userfile']['tmp_name'], - the name of the uploaded file on the client's machine $_FILES['userfile']['name'] does not work.

Categories