HTML in 'index.php':
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload" accept="application/pdf, .pdf" required><br>
<input type="submit" name="submit" value="SUBMIT">
</form>
PHP in 'index.php':
<?php
if (isset($_POST['submit'])) {
$target_dir = 'docs/';
$temp_loc = $_FILES['fileToUpload']['tmp_name'];
$file = $_FILES['fileToUpload']["name"];
if(move_uploaded_file($temp_loc,$target_dir.$file)) {
?><script>alert('successfully uploaded');</script><?php
} else {
?><script>alert('error while uploading file');</script><?php
}
}
?>
The following code doesn't seem to upload any of the documents I select to the target dircetory, that is, '/docs'.
I'm using wampserver with localhost, if that is needed at all. The 'file-upload' is allowed in the 'php.ini'
I've seen many tutorials and have even done troubleshooting on Stackoverflow itself, but I can't seem to make it work.
Thanks in advance
Related
I'm trying to upload a file to my php server, then return the name of the file to display in the html document. But I get the following
`error: Objektet wasn't found! The requested address was not found on this server. The link on the previous page appears to be incorrect or out of date Error 404
localhost
Apache/2.4.27 (Win32) OpenSSL/1.0.2l PHP/7.1.8`
My html Doc
<html>
<body>
<form method="post" enctype="multipart/form-data" action="server.php">
<input type="file" name="fileToUpload" id="fileToUpload" size="35">
<br>
<br>
<input type="submit" value="Upload" name="submit">
</body>
</html>
My php doc
<?php
header('Content-type: text/plain');
if(isset($_POST["fileToUpload"])){
$file = $_FILES["fileToUpload"];
echo("File: ".$file);
}
?>
You have many errors in PHP
<?php
if(isset($_FILES["fileToUpload"])){
$file = $_FILES["fileToUpload"]["name"];
echo "File: ".$file;
}
?>
HTML
<html>
<body>
<form method="post" enctype="multipart/form-data" action="server.php">
<input type="file" name="fileToUpload" id="fileToUpload" size="35">
<br>
<br>
<input type="submit" value="Upload" name="submit">
</body>
</html>
Errors
1.if(isset($_POST["file"])){ its not post it should be $_FILES["fileToUpload"]) since its a file upload
$file = $_FILES["file"]; and in your html you have defined file name as fileToUpload but your accessign unknown name so it should be $file = $_FILES["fileToUpload"]["name"];
In your PHP script you are asking for a form name that does not exist. In your form, the variable is called fileToUpload but in your script you are checking for $_POST['file'].
Also, the global $_FILES is an array with information about the file, so you cannot use echo to show its content. Use echo $_FILES['fileToUpload']['name'] since $_FILES['formFieldName']['name'] will display the original name of the file on the client machine.
I'm trying to build a file upload form and I'm having trouble with the very basics. My form is this:
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit"/>
</form>
</body>
</html>
My php code so far is one line and it doesn't do anything:
<?php
echo $_POST['filename'];
?>
The idea (at this point) is just to display the name of the file entered in the form. What am I doing wrong?
Based on your code I modified it. Have a try it.
HTML Part
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit" name="submit" />
</form>
</body>
</html>
PHP
if (isset($_POST['submit'])) {
// Check if files array is not empty
if (!empty($_FILES)) {
$imageName = $_FILES['filename']['name'];
echo $imageName;
// Insert your code related to upload
}
}
You can print the filename using the following code:
<?php
echo $_FILES["filename"]["name"];
?>
Recently I changed servers and my images uploading/resizing script (https://www.verot.net/php_class_upload.htm) stopped working, error message "Local file doesn't exist." The only change there was since it worked - I changed from $filetoUpload[$i] to $_FILES["fileToUpload"]["name"][$i].
Here's the script:
HTML
<form action="testupload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload[0]" id="fileToUpload">
<input type="file" name="fileToUpload[1]" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
<?php
// testupload.php
include('classes/class.images.inc.php');
$pictureslocation = "my images location/";
for($i=0;$i<=1;$i++){
if($_FILES["fileToUpload"]["name"][$i] != "none" AND $_FILES["fileToUpload"]["name"][$i] != ""){
$handle =new upload($_FILES["fileToUpload"]["name"][$i]);
if ($handle->uploaded) {
// Script doesn't get here but gives an error
}else{
echo 'error : ' . $handle->error;
// The error it gives here is "Local file doesn't exist."
}
}
}
Just a simple upload script works without problems.
I'm trying to take posted input file name, I'm not uploading anywhere.
I just need the name of posted filename so I'm trying this code;
<form method="post" enctype="multipart/form-data" role="form">
<input type="file" id="file" name="file">
<input type="submit" name="submit" value="Submit Form">
</form>
<?php
if(isset($_POST['submit'])){
echo $_FILES['file'];
}
?>
If I change enctype="multipart/form-data" into form tag, it's ok, but I need this tag.
You still need the enctype attribute, as the files will not be available without it.
if (isset($_POST['submit'])) {
echo $_FILES['file']['name'];
}
use
echo $_FILES['file']['name'];
instead of
echo $_FILES['file'];
$_FILES['file'] contains array of properties of uploaded file. use print_r instead. It will work fine.
you can get file name like that
$name = $_FILES['file']['name'];
this code is working fine
<form method="post" enctype="multipart/form-data" role="form">
<input type="file" id="file" name="file">
<input type="submit" name="submit" value="Submit Form">
</form>
<?php
if(isset($_POST['submit'])){
echo "<pre>";
print_r($_FILES['file']) ;
}
?>
Hi Hi trying to upload a file via a form move it to another folder and then print its name. But doesnt work don't know why.
<form method="post" action='exercice.php' id="form1">
<input type="file" name="files" id="files" onChange="submitForm();">
</form>
<?php
if (isset($_FILES['files']))
{
move_uploaded_file($_FILES['files']['tmp_name'], "uploaded/");
echo $_FILES['files']['name'];
}
?>
form missing:
enctype="multipart/form-data"
ref:
spec
<form method="post" action='exercice.php' id="form1">
<input type="file" name="files" id="files" onChange="submitForm();">
</form>
<?php
if (isset($_FILES['files']))
{
move_uploaded_file($_FILES['files']['tmp_name'], "uploaded/".$_FILES['files']['name']);
echo $_FILES['files']['name'];
}
?>
You need to include the file name in the upload path, like shown above.
Also, where is your upload (ajax) code?
Did isset($_FILES['files']) return true?