How do I get path from an uploaded file in PHP? [duplicate] - php

This is driving me crazy. I'm trying to figure out how to upload a file. I've got two very simple files, yet it doesn't seem to work. This first is the file that allows the user to choose the file:
<html>
<head>
<title>File Upload Form</title>
</head>
<body>
This form allows you to upload a file to the server.<br>
<form action="getfile.php" method="post"><br>
Type (or select) Filename: <input type="file" name="uploadFile">
<input type="submit" value="Upload File">
</form>
</body>
</html>
</code>
The second is the php file that handles it:
<html>
<head>
<title>Process Uploaded File</title>
</head>
<body>
<?php
print_r($_FILES);
print "<P>\n";
move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'],
"../blimages/site/7337/{$_FILES['uploadFile'] ['name']}")
?>
</body>
</html>
Since -- except for the print_r -- I pulled these off a website tutorial on how to do a file upload, I'd think these files are okay.
The print_r($FILES) return a completely empty array.
I also checked the php.ini. File uploads are allowed, and the max size is 2M, which I assume is 2 megabytes, which is far larger than the file I've been trying to upload.
What else could be wrong?
Thanks,
Sean.

Add the proper enctype attribute to your form tag:
<form action="getfile.php" method="post" enctype="multipart/form-data">
It's documented here: http://www.php.net/manual/en/features.file-upload.post-method.php
Also, make sure there's no space between your brackets when you access multi-dimensional arrays:
$_FILES['uploadFile']['tmp_name']

You shuold use attribute enctype="multipart/form-data" in form tag.

Add that in Form tag
enctype="multipart/form-data"

Related

Two submit buttons in one HTML form, for uploading or deleting a file with PHP

I tried to create one form containing two submit buttons, one to upload a file and the second to delete a selected file from a specific location. The upload process is successful, but the delete process fails.
This is the code for the two submit buttons:
<html>
<head>
<title>Upload and Delete file</title>
</head>
<body>
<form action="upanddel.php" method="post" enctype="multipart/form-data">
Select File to Upload:<input type="file" name="uploadfile" value="upload">
<input type="submit" name="upload" value="Upload File">
<input type="submit" name="delete" value="Delete File"><br><br>
</form>
</body>
</html>
And this is the PHP code for uploading or deleting a file:
<?php
if(isset($_POST['upload'])) {
onFunc();
}
if(isset($_POST['delete'])) {
offFunc();
}
function onFunc(){
$filenameupload=__DIR__.'\\upload\\'.$_FILES['uploadfile']['name'];
if(!file_exists($filenameupload)){
move_uploaded_file($_FILES['uploadfile']['tmp_name'],$filenameupload);
print_r( $_POST);
$_POST=array();
}
}
function offFunc(){
$filenamedelete=__DIR__."\\upload\\".$_FILES['uploadfile']['name'];
unlink($filenamedelete);
echo "file is deleted";
}
?>
The error is:
Warning: unlink(C:\wamp64\www\IntroducingPHP\upload\Events-News.xlsx): Resource temporarily unavailable in C:\wamp64\www\IntroducingPHP\Deletefile.php on line 5
What is the solution?
The path you mentioned has '\\'. From there and from the path mentioned in the warning it seems that you are using Windows operating system.
The file path provided to the method unlink() seems valid because the warning says the resource is temporary not available.
This means some process is having access to your file that's why it is not allowed to be deleted.
There is two possibilities :
Are you opening the file after it is uploaded? In that case if the file is open it won't be allowed to delete it. So close it and try to delete again.
If you've not opened the file, then there is the possibility that the variable you have used to refer to the file is still holding it.
Inside onFunc() you are using $filenameupload to refer the file.
If the file does not exist you upload the file and in the end you are clearing $_POST.
But the variable $filenameupload is not processed afterwards. So maybe it is holding the file. You add the line
unset($filenameupload);
after the if() condition inside onFunc() and check it.
This will remove the reference to the file if it is yet not removed and afterwards you may be able to delete the file.

how upload image in php store in some directory and display it

simple question I am learning php so i want to give option to user that it uploads the image(using upload button ) and it save in my system directory and my program display it on browser
Try the following:
1) form.php
<html>
<head>
<title>Upload (TEST)</title>
</head>
<body>
<form action="upload.php" method="post">
<input type="file" name="file">
</form>
</body>
</html>
2) upload.php
<?php
if(pathinfo("yourdir/" . basename($_FILES["file"]["name"]), pathinfo_extension) != 'jpg') { // and so on...
move_uploaded_file($_FILES["file"]["tmp_name"], "yourdir/" . $_FILES["file"]["name"]);
}
?>
You don't even need to care about the extension, as the browser will display your file as an image if it recons it actually is an image, so you don't have to get mad with exestions. However, for security purposes, it is better to always check it is not a potentially malicious file (php, py, js ...)

The requested URL [filename.php] was not found on this server

<?php
echo "hello";
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="/one1.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>
`
I have a Url on my server
This url is working. I have written echo "hello" in this.
but when i go through in this url i have written the html file upload code
It says The requested URL [one1.php] was not found on this server.
I checked my file_upload is on.
Make sure your 'action' attribute on form tag is set to /one1.php
I got the answer I contact the hosting team. They told to off the mod security in my panel and things the worked.

Upload file doesn't neither try to upload

I have a very simple script:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<!-- this is up.php -->
<form action="up.php" method="post" enctype="multipart/form-data">
<input type="file">
<input type="submit">
</form>
<?php print_r($_FILES); ?>
</body>
</html>
When I select a file and I click on "Submit" the page is istantly reloaded and nothing happens.
I'm trying with this little script because in a more complex page with other fields everything works well except for the file form. I mean, the database is filled with all the informations provided by my form, but images are not uploaded.
The problem is that the page neither try to upload them, I mean, it should upload them (see the percentage on bottom of page) and then pass it to PHP.
In my case instead the page is istantly submitted and reloaded without the attempt of load pictures.
I have other scripts on my server that upload images, and them works.
Why this one does not work? Thanks.
Edit: live demo here:
http://allise.net/up.php
Actually your file needs to be named most probably.
Try
<input type="file" name="file">
or whatever is set in your up.php file
You need <form method="post" ...> for the form to work.
And the file input should have a name attribute.

$_FILES empty when trying to do a file upload

This is driving me crazy. I'm trying to figure out how to upload a file. I've got two very simple files, yet it doesn't seem to work. This first is the file that allows the user to choose the file:
<html>
<head>
<title>File Upload Form</title>
</head>
<body>
This form allows you to upload a file to the server.<br>
<form action="getfile.php" method="post"><br>
Type (or select) Filename: <input type="file" name="uploadFile">
<input type="submit" value="Upload File">
</form>
</body>
</html>
</code>
The second is the php file that handles it:
<html>
<head>
<title>Process Uploaded File</title>
</head>
<body>
<?php
print_r($_FILES);
print "<P>\n";
move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'],
"../blimages/site/7337/{$_FILES['uploadFile'] ['name']}")
?>
</body>
</html>
Since -- except for the print_r -- I pulled these off a website tutorial on how to do a file upload, I'd think these files are okay.
The print_r($FILES) return a completely empty array.
I also checked the php.ini. File uploads are allowed, and the max size is 2M, which I assume is 2 megabytes, which is far larger than the file I've been trying to upload.
What else could be wrong?
Thanks,
Sean.
Add the proper enctype attribute to your form tag:
<form action="getfile.php" method="post" enctype="multipart/form-data">
It's documented here: http://www.php.net/manual/en/features.file-upload.post-method.php
Also, make sure there's no space between your brackets when you access multi-dimensional arrays:
$_FILES['uploadFile']['tmp_name']
You shuold use attribute enctype="multipart/form-data" in form tag.
Add that in Form tag
enctype="multipart/form-data"

Categories