Upload files in PHP doesn't work - php

I am new to PHP and I'm trying to make an upload script. But it doesn't work completely.
The thing that doesn't work is that when I have uploaded the photo it doesn't store the photo in the folder "uploads". (The folder location is: Applications > MAMP > htdocs > Marjolein)
Also I want to show the photo that has been uploaded in the browser, but this also doesn't work.
I work with a Mac and use MAMP to run my php code. Can you please help me so I can show the picture in the browser and that it will be stored in the folder "uploads"?
The code I have is:
uploader.php
<style>
.sucess{
color:#088A08;
}
.error{
color:red;
}
</style>
<?php
$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
$upload_exts = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($upload_exts, $file_exts))
{
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>";
// Enter your path to upload file here
if (file_exists("://Applications/MAMP/htdocs/Marjolein/uploads/" .
$_FILES["file"]["name"]))
{
echo "<div class='error'>"."(".$_FILES["file"]["name"].")".
" already exists. "."</div>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]);
echo "<div class='sucess'>"."Stored in: " .
"://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]."</div>";
}
}
}
else
{
echo "<div class='error'>Invalid file</div>";
}
?>
<?php
if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!='')
{
?>
<p><img src="uploads/<?php echo $_REQUEST['show_image'];?>" /></p>
<?php
}
?>
uploadform.html
<html>
<body>
<form action="uploader.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>
The webbrowser shows after I click on the submit button:
Upload: 0_8caab_996cc75d_orig.jpg
Type: image/jpeg
Size: 538.166992188 kB
Temp file: /Applications/MAMP/tmp/php/phptpCA8B
Stored in: ://Applications/MAMP/htdocs/Marjolein/uploads/0_8caab_996cc75d_orig.jpg

Try to echo the image after uploading: (DonĀ“t know about the storage of the image)
echo '<img src="://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]" border=0>';
Place it here:
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]);
echo "<div class='sucess'>"."Stored in: " .
"://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]."</div>";
echo '<img src="://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]" border=0>';
}
Maybe the path to the file is not correct. i think you can try this instead of your previous path:
move_uploaded_file($_FILES["file"]["tmp_name"],
"../uploads/" . $_FILES["file"]["name"]);
or if the folder "uploads" is located at in the same as the uploader.php you can use this:
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);

Related

Invalid File on file upload PHP

upload_file.php
$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma", "MP4");
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if ((($_FILES["file"]["type"] == "video/mp4")
|| ($_FILES["file"]["type"] == "audio/mp3")
|| ($_FILES["file"]["type"] == "audio/wma")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg"))
&& ($_FILES["file"]["size"] < 20000000)
&& 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";
}
?>
html
<!DOCTYPE html>
<head>
<title></title>
</head>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file"><span>Filename:</span></label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
So my problem is I get the message "Invalid File". This only happens when I try to upload video type files. However when I try to upload an image it works like a charm. I've searched all over stackoverflow for other video file upload codes and still couldn't find any that worked. Anyone that could refer another question/solution to me and/or fix this problem will greatly be appreciated.
EXTRA NOTE
I've already tried adding echo "Its type is " . $_FILES["file"]["type"]; to debug what file type is being given however it just returns a nice white space.
Change this part
else
{
echo "Invalid file";
}
to
else
{
echo "Invalid file";
echo "Its type is " . $_FILES["file"]["type"];
}
Now upload the files that don't work and add those types to your list
Apparently the problem only was because the php.ini was set to only accept 10M the file I was uploading was over 15MB and so I guess it gave me the error. But shouldn't that give me the error file-size is too much or something? But that's basically the reason I got the error. :)

Can't find Uploaded folder in php?

I've created two files namely (index.html) and (upload_file.php) in my "var/www/html/nns" folder. Here the codes....
//index.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>
//upload_file.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/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 200000)
&& 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"] / 200000) . " 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";
}
?>
Finally i've created myown folder namely "upload" in "var/html/nns/". In order to upload an image using index.html then it shows in....
Upload: new_bar.png
Type: image/png
Size: 0.211895 kB
Temp file: /tmp/phpjSn4x8
Stored in: upload/new_bar.png
But i've didn't find any image in my folder... what happens??? am I missing something???
Try using an absolute path to the upload folder rather than a relative one, such as:-
$source=$_FILES["file"]["tmp_name"];
$destination=$_SERVER['DOCUMENT_ROOT'].'/uploads/'.$_FILES["file"]["name"];
$result=#move_uploaded_file($source,$destination);
Make sure, you have provided the write permissions i.e 777 to your upload folder.
Add a dot and a dash in front of the folder name.
./upload/
If the above solution is not working, try changing the permission of the upload folder.

Uploading photo in Php

I've been trying to do a photo upload using Php. What I am seeing when I do a submit is, the page keeps loading infinitely.
End result the photo is not being uploaded to the directory.
Here is the HTML Code :
<!DOCTYPE html>
<html>
<head>
<title>Photo Upload</title>
</head>
<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>
Php Script :
<style>
.sucess{
color:#088A08;
}
.error{
color:red;
}
</style>
<?php
$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
$upload_exts = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($upload_exts, $file_exts))
{
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>";
// Enter your path to upload file here
if (file_exists("uploads/" .
$_FILES["file"]["name"]))
{
echo "<div class='error'>"."(".$_FILES["file"]["name"].")".
" already exists. "."</div>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
echo "<div class='sucess'>"."Stored in: " .
"uploads/" . $_FILES["file"]["name"]."</div>";
}
}
}
else
{
echo "<div class='error'>Invalid file</div>";
}
?>
I've taken this code sample from Here.
I cannot understand what is going wrong here, but I think it has something to do with the path, but I am not sure.
Some help will be appreciated.
*I've edited the code and changed it to 'uploads/' .
It now works!
I checked it out use Relative Path Instead of absolute path then there will be no problem which ever environment you are working in local or server
eg:Instead of this C:\Users\Priyabrata\PhpstormProjects\FileUpload/uploads/
Use uploads/
It will work
when you host it on server some hosting providers will not provide the access so you have to change the folder permissions to writable and it will work cheers
See you are reffering like C:\\Users\\Priyabrata\\PhpstormProjects\\FileUpload/uploads/.Maybe this leads to the problem.
Replace it with the actual server link you use ..
I have tried your code and it works fine for me (I used different path since I am on Ubuntu). Try specifying your path without double slashes. (Like in the sample link you provided). Also make sure the folder exists ;)

Enable permissions on a folder to allow file upload.

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

uploading php script instead of chosen file

I have two simple starter scripts (html and php) to choose a file from the machine and then upload to server. However when I press upload, it appears to be uploading the actual php script that deals with the upload. here they are:
<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
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_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";
}
?>
THis i'd imagine is just a silly mistake somewhere on my part, but this is my first experience in uploading a file using php. thanks very much.

Categories