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().
Related
Hi there I'm trying to create an upload form in php. Containing, upload input, text input (name to be given to the file that was uploaded), and submit button. However I don't know much about php, so I don't know how to actually link that what I had typed on the <input type="text"/> becomes the name of the file when uploaded. If someone can help ? Thanks.
Here is my code:
<!DOCTYPE html>
<html>
<head>
<title>Tu peux uploader ici ta video.</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="text" name="file-name"></input><br /> <!-- [ASK]: How to make this the file name of the uploaded file -->
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?PHP
if(!empty($_FILES['uploaded_file']))
{
$path = "uploads/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
move_uploaded_file ( string $filename , string $destination ) takes filename which is the filename of the uploaded file and destination is the destination of the moved file. Note that destination directory must exist; move_uploaded_file() will not automatically create it. So lets get the name now...
$_FILES['uploaded_file']['tmp_name'] gives you the temporary filename of the file in which the uploaded file was stored on the server eg. C:\Users\USER\AppData\Local\Temp\phpD3C.tmp
while
$_FILES['uploaded_file']['name'] gives you the actual name which you need to extract the extension eg. myfile.jpg
To link that what you had typed on the , first get it via $_POST["file-name"], then concatenate with the extension. Use pathinfo to retrieve the original extension.
Change your code to become...
<!DOCTYPE html>
<html>
<head>
<title>Tu peux uploader ici ta video.</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="text" name="file-name"></input><br /> <!-- [ASK]: How to make this the file name of the uploaded file -->
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(!empty($_FILES['uploaded_file']))
{
$file_parts = pathinfo(basename( $_FILES['uploaded_file']['name'])); //access the actual name instead of tmp_name
//just pick the extension of the file_parts and concatenate it to your path
$path = 'images/';
$path = $path . $_POST["file-name"] . "." . $file_parts['extension'] ;
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename($path) ." has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
}
?>
This should get you the desired name. So if you upload file dog.php and in the text field you have cow, the resulting name should be cow.php.
Result:
I am trying to use simple php script to upload an image
The code below :
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
<title>Upload your files</title>
</head>
<body>
<form enctype="multipart/form-data" action="uploadfile.php" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?php
//echo exec('whoami');
//die($_FILES['uploaded_file']);
if(!empty($_FILES['uploaded_file']))
{
$path = "./img/backend/uploads/enquiry/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
I'm using php 7.1 I've already checked the folder for permissions. But when I try to submit it take me to some page with #custom kind of stuff in url. but if I try to post it without image it works fine.
What is the problem ? I think about apache which is not handle that properly. On my other server its working fine.
Just on this server I use auto ssl, is that the problem ?
See the image it take me like this link looks like its not submitting or rejected by server
Please change the path to this
$path = __DIR__."/img/backend/uploads/enquiry/";
You are missing a dot in the file path. Try the following:
$path = "./img/backend/uploads/enquiry/";
Full code:
<!DOCTYPE html>
<html>
<head>
<title>Upload your files</title>
</head>
<body>
<form enctype="multipart/form-data" action="#" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?php
if(!empty($_FILES['uploaded_file'])){
$path = "./img/backend/uploads/enquiry/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
echo $path;
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
Without dot the file path will take script name as well, for example, if your PHP script/file name is "test.php", then without a leading dot in file path will create a path like (www is the folder where your test.php file is saved):
/www/test.php/img/backend/uploads/enquiry/
And hence it will be an invalid file path, with a leading dot it will skip it (layman terms) and your path will be like:
/www/img/backend/uploads/enquiry/
And this path is a valid one, hence it will upload.
i'ma beginner and i'm trying to upload a file to uploads/ folder from this tutorial but as i run my program after submit it's showing error There was an error uploading the file, please try again! can you tell me what's wrong here??
form.php:
<html>
<head>
<title>Form image</title>
</head>
<body>
<form enctype="multipart/form-data" action="uploader.php" method="post" name="changer">
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
<input type="file" name="uploadedfile" accept="image/jpeg">
<input type="submit" value="Submit">
</form>
</body>
</html>
uploader.php
<?php
//where the file is going to be placed..
$target_path="uploads/";
$target_path=$target_path.basename($_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['name'], $target_path)){
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
The correct code is
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)
You are using
move_uploaded_file($_FILES['uploadedfile']['name'], $target_path)
This will not work since the file is stored in the temp folder after the upload is initiated and while moving the file it must know the temp name so that the file could be moved from temp folder to the server.
So I am trying to upload files to the database through php here is the code
HTML:
<!doctype html>
<html>
<head>
<title>Image Upload</title>
</head>
<body>
<form action="uploadimage.php" method="POST" enctype="multipart/form-data">
<input type="text" name="tag"/>
<input type="file" name="file"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
php:
<?php
$target = "files/";
$target = $target . basename( $_FILES['file']['name']);
$name=$_POST['tag'];
$file=($_FILES['file']['name']);
mysql_connect("localhost","Bluesir9","Bluesir9","website");
mysql_query("Insert into files values('$name','$file');");
if(move_uploaded_file($_FILES['file']['tmp_name'],$target))
{
echo "The File ".basename($_FILES['file']['name'])."has been uploaded";
}
else
{
echo "Sorry there was an error";
}
?>
when I check the target folder the files are there, but I cant find the files on the database returns an empty result set when I query it through phpMyAdmin.
What am I doing wrong?
Since the target files are there in folder, and if you can't find the files on the database then obvious thing should be to check insert query.
Please check syntax of insert query
mysql_query("Insert into files values('$name','$file')");
I am trying to upload a small mp3 through a simple html form using PHP.
Here is my html:
<html>
<head></head>
<body>
<form action="upload_mp3.php" method="post" enctype="multipart/form-data">
<input type="file" name="mp3" />
<input type="submit"/>
</form>
</body>
</html>
Here is my php:
<?php
$mp3 = ($_FILES['mp3']['name']);
$target = "mp3/";
$target = $target . basename( $_FILES['mp3']['name']);
if(move_uploaded_file($_FILES['mp3']['tmp_name'], $target))
{ header("Status: 200"); }
else
{ echo "no";}
?>
Does anything look wrong with the code. Also could it be that my temp file isn't writable? If so can I get some instructions on how to make it writable.
Thanks!
Make sure your form tag has the enctype=multipart/form-data attribute set:
<form action="upload_mp3.php" method="POST" enctype="multipart/form-data">