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"];
}
Related
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"]);
I need some advice and guide line.
I am trying to upload image into a folder using link. And I am unable to upload an image.
I am trying this code. This shows error message :
[function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if (file_exists("http://localhost.myimage.com/uploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"http://localhost.myimage.com/uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
?>
<html>
<body>
<form action="" 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>
Is it possible ?
The destination of the moved file needs to be an absolute path to the server, i.e.
move_uploaded_file( $_FILES["file"]["tmp_name"], $_SERVER['DOCUMENT_ROOT'] ."/uploads/" . $_FILES["file"]["name"]);
You are supplying file_exists and move_uploaded_file with an URL. Try supplying a file on the hard drive instead. Change:
file_exists("http://localhost.myimage.com/uploads/" . $_FILES["file"]["name"])
to
file_exists($_SERVER['DOCUMENT_ROOT'] . "uploads/" . $_FILES["file"]["name"])
and similar to move_uploaded_file since that's where the error originated.
No it's not possible this way.
You try to store the uploaded file on a wrong destination.
I assume you own "http://localhost.myimage.com/uploads/" and wan't to store the file under '/uploads/' in your server.
If your project is installed e.g. under /var/www/my_prj/
And your upload is under /var/www/my_prj/uploads
then your code should look like:
move_uploaded_file($_FILES["file"]["tmp_name"],
"/uploads/" . $_FILES["file"]["name"]);
This only applies when '/var/www/my_prj/' is the DocumentRoot for your apache configured host.
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>
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.