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"]);
Related
(The question was initially asked in Server Fault. I move it to here, because I don't have enough credit to start a bounty over there.)
I am trying to make my Windows Server 2012 R2 to host a webpage where users could upload a file.
<!-- indexfile.html -->
<form action="uploadfile.php" method="post" enctype="multipart/form-data">
Browse for File to Upload: <br>
<input type="file" name="file" id="file" size="80"> <br>
<input type="submit" id="u_button" name="u_button" value="Upload the file">
</form>
// uploadfile.php
<?php
$file_result = "";
if ($_FILES["file"]["error"] > 0)
{
$file_result .= "No File Uploaded or Invalid File ";
$file_result .= "Error Code: " . $_FILES["file"]["error"] . "<br>";
} else {
$file_result .=
"Upload: " . $_FILES["file"]["name"] . "<br>" .
"Type: " . $_FILES["file"]["type"] . "<br>" .
"Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br>" .
"Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (chmod("C:/inetpub/testaddbbacom/test", 0777))
$file_result .= "chmod sucessful!<br>";
else
$file_result .= "chomod NOT sucessful!<br>";
copy($_FILES["file"]["tmp_name"], "C:/inetpub/testaddbbacom/test/" . $_FILES["file"]["tmp_name"]);
$file_result .= "File Upload Successful!";
echo $file_result;
}
?>
The problem is, files can be uploaded into C:\Windows\Temp\, whereas copy does not work. As consequence, C:\inetpub\testaddbbacom\test is always empty. I did try to change the permission of this folder by chmod, but it does not really work. Here is a comparaison of the permissions of two folders:
I also tried move_uploaded_file, but it did not work either, probably due to the same permission reason...
Does anyone know how to solve this problem?
Replace,
copy($_FILES["file"]["tmp_name"], "C:/inetpub/testaddbbacom/test/" . $_FILES["file"]["tmp_name"]);
With the following,
move_uploaded_file($_FILES["file"]["tmp_name"], "C:/inetpub/testaddbbacom/test/" . $_FILES["file"]["name"]);
the difference is use file name i.e $_FILES["file"]["name"]) and not $_FILES["file"]["tmp_name"]) in destination path.
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 am trying to use PHP to upload a CSV file (type:application/vnd.ms-excel) to my Unix box. And then FTP it to Mainframes.
The problem occurs in uploading when extra blank lines are added to the uploaded file at the end of each line, i can see this when i download the uploaded CSV file using ftp Command in cmd prompt.
No Extra lines appear in the CSV while viewing in Unix box.
When it FTPs it to mainframe i can see a dot added to each line. Hex Dec x'0D'
Could you please help me out in truncating this extra character while uploading?
My HTML File
<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>
My PHP Code "upload_file.php"
<?php
$allowedExts = array("csv", "CSV");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["type"] == "application/vnd.ms-excel")
&& in_array($extension, $allowedExts))
{
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 />";
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"]);
$conn = ftp_connect("a.b.c.com") or die("Could not connect");
ftp_login($conn,"login","pass");
echo ftp_put($conn,"'dataset.name'","upload/" . $_FILES["file"]["name"],FTP_ASCII);
ftp_close($conn);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
You might want to take a look at the http://at2.php.net/manual/en/function.trim.php method.
You can open the file after storing it, iterating over each line and trim() them. You can print them back to the file (or just load everything, iterante and do a file_put_contents()). This should remove any whitespace from the beginning and the end before you upload it.
If you just want to remove it from the end, you can also use rtrim() or if you want to be more flexible you can do it with preg_replace() and something like /\s*$/.
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>