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">
Related
I'm trying to add an upload feature in an existing Wordpress plugin. But the file seems not to load. I use this code in the form:
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
// other code of existing plugin.
<input type="submit" value="plugin submit" id="plugin submit" name="plugin submit">
</form>
Then in the function of the plugin I put the test line for knowing what happens:
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"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
I enter in else branch but all fields are empty. What am I missing?
You will hit the else branch even if $_FILES is undefined.
You can first check that the user uploaded a file.
is_uploaded_file()
Returns TRUE if the file named by filename was uploaded via HTTP POST.
You can use somthing like:
if(is_uploaded_file($_FILES['file']['tmp_name'])
But since it seems this will not return TRUE in your case I must ask - since your form is POSTing to the same page that it is on - is your PHP code at the top of the page where your form resides? If that is the case don't you find it odd that it outputs the blank upload information even before you upload?
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' />
I have an image id='canvasImg' and I can not figure out how to take this image and save it to the server.
I tried saving the contents to a form and then putting the contents in a png file after decode but it didn't work.
You will need to use a simple file form upload
HTML
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
PHP (upload_file.php)
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"];
}
I'm new to the concept of file uploading using a form in PHP. I would like to know a PHP script for the purpose of uploading a logo image onto a site. I tried couple of sample demos available on the web but I didn't get the image into my folder.
PS: I didn't understand many of the scripts given.
I would like to upload a logo image to replace my existing logo. How to do that?
I have a link 'Change Logo' on clicking which it should go to another page where I can use a file uploading form. {I would prefer if the form didn't have an action.} But where will the saved image be posted. I would like a customized script that would upload an image into my folder 'style/images/' as 'logo.png'.
Check out the following link, it will show a step by step procedure of file upload through the clear comments in the script itself. Hope this helps
http://www.reconn.us/content/view/30/51/
Step 1 - Copy paste the above script and save it in a folder
Step 2 - Create a folder with name images in the same folder where the .php file with the above script pasted.
Step 3 - Upload a file sizes not greater than 100kb. The output should be File Uploaded Successfully! Try again!
Step 4 - To check whether the file is uploaded, check the images folder, you will find the uploaded file in the folder.
OR Try this
<?
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
// 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("images/" . $_FILES["file"]["name"]))
{
$_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"images/" . $_FILES["file"]["name"]);
echo "Stored in: " . "images/" . $_FILES["file"]["name"];
}
}
?>
<html>
<body>
<form action="index.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" class="validate[required] text-input" />
<input type="submit" name="submit" value="Attach file" >
</form>
<form id="form1" name="form1" action="">
<input type="hidden" id="filename" value="<?echo $_FILES["file"]["name"];?>"/>
</form>
</body>
</html>
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.