I have the below PHP script processing my form.
Why am I not seeing the file in the specified location?
Is there something I am doing wrong with regards to the file's location?
<html>
<head>
<title>Upload</title>
</head>
<body>
<form enctype="multipart/form-data" action="uploadFile.php" method="post">
<input type="file" name="file" id="file">
<br>
<input type="submit">
</form>
</body>
<?php
echo "Processing...<br>";
$fileResult = "";
if($_FILES["file"]["error"] > 0)
{
$fileResult .= "No File Uploaded";
$fileResult .= "Error Code: " + $_FILES["file"]["error"];
} else
{
$fileResult .=
"Upload:" . $_FILES["file"]["name"] . "<br>" .
"Type:" . $_FILES["file"]["type"] . "<br>" .
"Size:" . $_FILES["file"]["size"] . "<br>" .
"Temp File:" . $_FILES["file"]["tmp_name"] . "<br>";
move_uploaded_file($_FILES["file"]["tmp_name"], "/home6/schne.../public_html/FileStore/Data/". $_FILES["file"]["name"]);
$fileResult .= "File Uploaded";
}
echo $fileResult;
?>
If the problem isn't server-side, there's two places to easily go wrong.
You may have forgotten then enctype attribute in your form. Your upload form should look like this:
<form method='post' enctype='multipart/form-data'>
Also, make sure you have a MAX_FILE_SIZE submitted in the $_POST array:
<input type='hidden' name='MAX_FILE_SIZE' value='100000' />
Related
Im new to PHP and still learning, but I want to know if I am able to do something like this. I have a basic HTML form like the one below.
<form action="upload.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">
<label><input type="checkbox" id="cb" value="checkb">Make Private.</label>
</form>
And a basic upload PHP document like the one below as well.
<?php
if ($_FILES["file"]["size"] < 2097152) {
if ($_FILES["file"]["error"] > 0) {
header('Location: /');
}else {
echo "Original Name: " . $_FILES["file"]["name"] . "<br>";
echo "File Type: " . $_FILES["file"]["type"] . "<br>";
echo "File Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
$randprefix = hash('ripemd160', openssl_random_pseudo_bytes(32), false);
$filepath = "upload/" . $randprefix . $_FILES["file"]["name"];
if (file_exists($filepath)) {
echo $filepath . " already exists. ";
}else {
move_uploaded_file($_FILES["file"]["tmp_name"], $filepath);
echo "Link to file: " . '' . $filepath . '' ;
}
}
}else {
echo "File too large.";
}
?>
What i'm asking is, is it possible to have a checkbox on the HTML form that when checked the file will upload to a folder named "private" and when unchecked the file will upload to a folder named "public". Thank you.
You can use the condition like this so that than only you can get the checkbox value after the form is submitted.
if(isset($_POST['check']))
{
$filepath = "upload/private" . $randprefix . $_FILES["file"]["name"];
}
else
{
$filepath = "upload/public" . $randprefix . $_FILES["file"]["name"];
}
You have to place the name for the checkbox input type so that you can check in the upload.php page and then set the folder over there.
Replace:
<label><input type="checkbox" id="cb" value="checkb">Make Private.</label>
With:
<label><input type="checkbox" id="cb" value="checkb" name="check">Make Private.</label>
Explanation: When the user selects the checkbox at the time of submitting it will check for the check box value and it will make the $filepath as upload/private or else if the user while submitting the data he/she has not selected the check box it will change the code as per the selection if the check box value. So that you can move_upload_file() based on the selection available.
<form action="upload.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">
<label><input type="checkbox" id="cb" name="checkb" value="checkb">Make Private.</label>
</form>
<?php
if ($_FILES["file"]["size"] < 2097152)
{
if ($_FILES["file"]["error"] > 0)
{
header('Location: /');
}
else
{
echo "Original Name: " . $_FILES["file"]["name"] . "<br>";
echo "File Type: " . $_FILES["file"]["type"] . "<br>";
echo "File Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
$randprefix = hash('ripemd160', openssl_random_pseudo_bytes(32), false);
if($_POST['checkb'] == "checkb")
{
$filepath = "private/" . $randprefix . $_FILES["file"]["name"];
}
else
{
$filepath = "upload/" . $randprefix . $_FILES["file"]["name"];
}
if (file_exists($filepath))
{
echo $filepath . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], $filepath);
echo "Link to file: " . '' . $filepath . '' ;
}
}
}
else
{
echo "File too large.";
}
?>
I think it is work I test It and you haven`t problems to copy!
I'm trying to upload and save file in dir called images through a form. Form looks like this:
<form name="spremi" action="spremaj.php" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="image" />
<input type="submit" value="Send" name="send" />
</form>
And php script looks like this:
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]);
echo "Stored in: " . "images/" . $_FILES["file"]["name"];
}
Script always prints message "already exists". I know that questions like this were already asked, but none of the answers helped me. This code didn't work either on localhost, or web server. Thank you.
In your html form you do not have a file parameter so your php needs to accept image so change
if (file_exists("images/" . $_FILES["image"]["name"]))
{
echo $_FILES["image"]["name"] . " already exists. ";
}else
move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]);
to
if (file_exists("images/" . $_FILES["image"]["name"]))
{
echo $_FILES["image"]["name"] . " already exists. ";
}else
move_uploaded_file($_FILES["image"]["tmp_name"],"images/" . $_FILES["image"]["name"]);
My html form:
<form action='' method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
My php file:
if ($_POST['submit'] == "Submit") {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("/downloads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/downloads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "/downloads/" . $_FILES["file"]["name"];
}
}
there are 2 folders in my public_html: tmp and downloads, both 777 permissions (just to test)
this file is located in a .htaccess protected folder within downloads (public_html/downloads/new/update.php)
and i want the zip files to be uploaded in the downloads dir.
This code won't give me any errors, but does not upload the file. Why?
Try adding
if(move_uploaded_file(...)){
echo "it works";
} else {
echo "NOPE";
}
And
replace:
move_uploaded_file($_FILES["file"]["tmp_name"], "/downloads/" . $_FILES["file"]["name"]);
with:
move_uploaded_file($_SERVER['DOCUMENT_ROOT'].'/'.$_FILES["file"]["tmp_name"], $_SERVER['DOCUMENT_ROOT']."/downloads/" . $_FILES["file"]["name"]);
PHP needs MAX_FILE_SIZE to receive uploaded files
<form action='' method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="10240000">
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
with this form you can upload a zip archiv with 10 MB or less...
you must also set the max_post_size and upload_max_filesize in your php.ini to the same or a higher value to upload files
use this PHP code
if ((isset($_POST['submit']) && $_POST['submit'] == "Submit") AND isset($_FILES)) {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("/downloads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
if(move_uploaded_file($_FILES["file"]["tmp_name"],
"/downloads/" . $_FILES["file"]["name"]))
echo "Stored in: " . "/downloads/" . $_FILES["file"]["name"];
else echo "file could not be processed";
}
}
This is how i solved it:
HTML:
<form enctype="multipart/form-data" method="post" action="">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
PHP:
if($_POST['submit'] == "Submit") {
$filename = $_FILES["file"]["name"];
$source = $_FILES["file"]["tmp_name"];
$type = $_FILES["file"]["type"];
$name = explode(".", $filename);
$target_path = "../".$filename;
if(move_uploaded_file($source, $target_path)) {
$message = "Your .zip file was uploaded";
} else {
$message = "ERROR";
}
if($message) echo $message;
}
My HTML FORM code:
<form action="upload.php" method="GET" enctyped="multipart/form-data">
<label for="file"> Filename:</label>
<input type="file" name="loadedFile" id="file"/></br>
<input type="submit" name="uploadItNow" value="Submit"/>
hai, this is this the uploader.
</form>
PHP File uploader script:
if (isset($_GET['uploadItNow'])) // checks if submit button has pressed
{
if ($_FILES['loadedFile']["error"] > 0)
echo "Error: ". $_FILES["loadedFile"] ["error"]. "</br>";
else
{
echo "Upload: ". $_FILES["loadedFile"] ["name"]. "</br>";
echo "Type: ". $_FILES["loadedFile"] ["type"] . "</br>";
echo "Stored in: " .$_FILES["loadedFile"] ["tmp_name"];
echo "Size: ". ($_FILES["loadedFile"] ["size"] / 1024). " Kb</br>";
//Copies file from TEMP_PHP dir to d.default dir
if (file_exists("." . $_FILES["loadedFile"]["name"]))
{
echo $_FILES["loadedFile"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["loadedFile"]["tmp_name"],"." . $_FILES["loadedFile"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["loadedFile"]["name"];
}
}
}
For some reason it doesn't work, my scrip keeps going to this code:
echo $_FILES["loadedFile"]["name"] . " already exists. ";
You need to use POST as well as change "enctyped" to "enctype".
<form action="upload.php" method="POST" enctype="multipart/form-data">
Try to change form method to POST
<form action="upload.php" method="POST" enctype="multipart/form-data">
Try using POST instead of GET
Maybe the file really does exist? And you're not thinking about the paths
Change method to post and enctyped to enctype
<form action="upload.php" method="POST" enctype="multipart/form-data">
Does anyone know any good tutorial on how to upload a file with php and save the files path to a sql server?
To upload a file you need at least a HTML POST form with multipart/form-data encoding. Therein you put an input type="file" field to browse the file and a submit button to submit the form.
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>
In the upload.php the uploaded file is accesible by $_FILES with the field name as key.
$file = $_FILES['file'];
You can get its name as follows:
$name = $file['name'];
You need to move it to a permanent location using move_uploaded_file(), else it will get lost:
$path = "/uploads/" . basename($name);
if (move_uploaded_file($file['tmp_name'], $path)) {
// Move succeed.
} else {
// Move failed. Possible duplicate?
}
You can store the path in database the usual way:
$sql = "INSERT INTO file (path) VALUES ('" . mysqli_real_escape_string($path) . "')";
// ...
From http://www.w3schools.com/php/php_file_upload.asp
HTML
<html>
<body>
<form action="upload_file.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>
PHP
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; //<- This is it
}
}
?>
Note that to upload the file you need to specify the path to save the file. If you save the file you already know it path.