I am trying to create a simple php image uploading form. I appear to have some kind of permissions problem though and I'm not sure how to fix this...
Here's my code...
<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>
upload_file.php...
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000))
{
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("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"];
}
}
}
else
{
echo "Invalid file";
}
?>
Here's the error I get...
Stored in: upload/81350042.jpgPHP Warning: move_uploaded_file(upload/81350042.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in D:\websites\tb123654\www\upload_file.php on line 27 PHP Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\upload\php5BFB.tmp' to 'upload/81350042.jpg' in D:\websites\tb123654\www\upload_file.php on line 27
open your ftp client, right click on the folder you upload things to, attributes or propieties, set permission to 777.
or
chmod 777 yourfolder
There is no folder called "uploads" in "D:\websites\tb1234\www\" so the images can't be moved there. I think that's what your script is trying to do. Try to use absolute paths and see if that works.
Related
I'm using xampp on a mac. I have the following script which I'm trying to use to upload a file to a folder:
upload.php
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& 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>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<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"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
The upload form is simply:
index.html
<html>
<body>
<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">
</form>
</body>
</html>
My folder structure in htdocs is:
-/htdocs
-index.html
-upload.php
-/upload
When I run the script I get the following error:
Warning: move_uploaded_file(upload/dirt.png)
[function.move-uploaded-file]: failed to open stream: Permission
denied in
/Applications/XAMPP/xamppfiles/htdocs/development/Templates/PHP&JS/Upload
Files to Server/form upload/upload.php on line 29
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to
move '/Applications/XAMPP/xamppfiles/temp/phpAQWDB1' to
'upload/dirt.png' in
/Applications/XAMPP/xamppfiles/htdocs/development/Templates/PHP&JS/Upload
Files to Server/form upload/upload.php on line 29 Stored in:
upload/dirt.png
How do I change the properties of the upload folder to allow uploading of files? I there a config file I should include to enable this?
You need to run the command
chmod -Rf 777 FOLDER_PATH
here FOLDER_PATH is the your folder path where the images get stored
i simply want that after i upload an image it will be redirected to a page that displays all the images. The images are saved in a folder. i have found some examples but they are so complicated, i just want to try the simple way first.
this is the HTML code for uploading image:
<form action="upload_image.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
Title:<input type="text" name="title" id="title" />
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
this is the PHP code to save the other details in the database:
include('../IFM-mobile_website/include/connect.php');
$title=$_POST['title'];
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
)
{
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"]) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<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"]);
echo "Stored in: " . "../IFM-mobile_website/upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
any help would be much appreciated.
Your file uploaded path and the path in which you are showing image are different
uploading to : "upload/" . $_FILES["file"]["name"]
showing it from : "../IFM-mobile_website/upload/" . $_FILES["file"]["name"]
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "../IFM-mobile_website/upload/" . $_FILES["file"]["name"];
To show all the images in a folder :
$a = glob('Your/path/*.{jpg,gif,png}',GLOB_BRACE);
print_r($a);
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*$/.
The following form is on my page html page
<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>
This is the script for the php
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000000000))
{
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("pics/2012/Blackhall Primary/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"pics/2012/Blackhall Primary/" . $_FILES["file"]["name"]);
echo "Stored in: " . "pics/2012/Blackhall Primary/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
When I press select on the form, nothing. It was working fine. The page with the form is called upload pictuture.html and the scriptt is called upload_file.php they are both on the root and the folder pics/2012/blackhall primary does exist and the pics dir is on the root also.
This was working but is not, can anyone see any errors I've made.
Thanks
Ross
Change your privileges and by whom script is executed (usually www-data or apache2) - this account must has write and probably read access to directory, where pictures are uploaded.
Anyway, checking file type based on $_FILES["file"]["type"] isn't safe. This can be easily falsified by hacker.
I used the codes(html+php script) below to upload image file
submit.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 upload file upload_file.php
<?php
echo $_FILES["file"]["type"] ;
echo "<br>";
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
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("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"];
}
}
}
else
{
echo "Invalid file";
}
?>
but it reported error:
image/jpeg
Upload: 02.jpg
Type: image/jpeg
Size: 54.2626953125 Kb
Temp file: /tmp/phpT617qx
Warning: move_uploaded_file(upload/02.jpg): failed to open stream: No such file or directory in /home/virtual/site18/fst/var/www/html/test/upload_file.php on line 29
Warning: move_uploaded_file(): Unable to move '/tmp/phpT617qx' to 'upload/02.jpg' in /home/virtual/site18/fst/var/www/html/test/upload_file.php on line 29
Stored in: upload/02.jpg
submit.html and upload_file.php are in same directory with mod 777
Welcome any comment
It could be because you should assign correct permissions for writing files into that file.
What are the permissions on upload/? Does the directory exist?