PHP upload script works locally, however not on remote server - php

It's strange, this script works locally (on MAMP), but not on my remote server (Bluehost). I tried adding some POST data, and that worked, however the file upload isn't working at all. Any ideas?
//upload file
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 2000000) && isSet($_FILES["file"])){
if ($_FILES["file"]["error"] > 0){
echo "<div class='error'>Return Code: " . $_FILES["file"]["error"] . "</div>";
}else{
if (file_exists("../upload/" . $_FILES["file"]["name"])){
echo "<div class='error'>" . $_FILES["file"]["name"] . " already exists. </div>";
}else{
move_uploaded_file($_FILES["file"]["tmp_name"],
"../upload/" . $_FILES["file"]["name"]);
}
}
}else{
if((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg"))){
//echo "<div class='error'>Invalid file</div>";
}else{
if(isSet($_FILES["file"]["type"]) && !isSet($message)){
echo "<div class='error'>Invalid file type.</div>";
}
}
}
Here is the upload form.
<form action="index.php" method="post" enctype="multipart/form-data">
<div>
<input type="file" name="file" id="file" />
<input type="submit" id="submit" value="Submit" />
</div>
</form>
The upload folder's permissions are set to "775".
I'd appreciate any help/ideas.
Update
Problem solved. It was a php.ini issue. I'm not sure where the problem was exactly. However, when I restored the default php.ini, file it worked.

I think problems is with the read-write-execute permission of the upoading folder in server. Make sure that the destination folder have read-write-execute(777) permissions.

Related

Upload image with caption and store caption & session username to database

I want to implement a simple image uploader that stores a caption, the user logged in ($_SESSION['username']) and the path that the file is saved.
The session is working, the code is correct, the database table exists, but it writes only the 'path' value, the 'username' and 'caption' remain empty on the database.
This is the code for uploading:
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"] < 5000000)
&& in_array($extension, $allowedExts)) {
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"]);
$caption = $_POST['caption'];
$uploaderUsername = $_SESSION['username'];
$path = "upload/" . $_FILES['file']['name'];
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());
mysql_query("INSERT INTO images (caption,username,path) VALUES
('$caption','$uploaderUsername','$path')");
header('Location: members.php');
}
}
} else {
echo "Invalid file";
}
I guess there is something wrong in this part:
$caption = $_POST['caption'];
$uploaderUsername = $_SESSION['username'];
$path = "upload/" . $_FILES['file']['name'];
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());
mysql_query("INSERT INTO images (caption,username,path) VALUES
('$caption','$uploaderUsername','$path')");
header('Location: members.php');
To add to my note above
"I don't see you calling session_start() before attempting to access the session variables. (which just happen to be one of the two variables you aren't getting :O). And in your form make sure you are setting the name='caption' for the caption input"
using the code you provided, to implement the caption you would want to do something along the lines of
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<label for="caption">Caption: </label>
<input type="text" id="caption" name="caption" placeholder="Add a caption here"><br>
<input type="submit" name="submit" value="Submit">
</form>

Upload Image on server using php but it doesn't exsist on server

I am really confused I have this image upload code and it's working fine on my home server "Xampp" and when I click on upload button it upload image and send it to Upload folder but When I upload this php and html page to server and works fine but it can't save image to Upload folder on server please help me out. Thanks
you can try it on my site
http://bing.freevar.com/image_upload.html
Here is 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>
Here is a PHP file
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
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"] / 10024) . " 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";
}
chmod("upload", 0644);
?>
If it works on your local server, but not on production you need to make sure that the folder exists on the production server and that it can be written to by the user account that the PHP script is executing under.
I think you got this PHP script from this link of w3schools.com. This php script has two extra parentheses in if condition. Remove these extra parentheses.
if (/*removed*/($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/x-png")// removed
&& ($_FILES["file"]["size"] < 200000)
&& in_array($extension, $allowedExts))
It worked for me. Hope it will work for you also. I think you need more validation. You can use getimagesize() function to check width, height, MIME type, attr of the uploaded image.

PHP File Upload Does Nothing

I've had several upload forms working before, however, even after almost copying my previous code this on doesn't seem to work, I prefer doing it all in one php script file and so it is all generated in this single file.
My form:
<form action="" method="post" enctype="multipart/form-data">
<ul>
<li>
<label for="file">File : </label>
<input type="file" id="file" name="file" required="required" />
</li>
<li>
<input type="submit" value="Upload" />
</li>
</ul>
</form>
My php upload:
if(!empty($_POST['file']))
{
echo "Found.";
$exts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$ext = end($temp);
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"] < 20000)
&& in_array($ext, $exts))
{
if($_FILES["file"]["error"] > 0)
{
$result = "Error Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
$scandir = scandir("/images/news/");
$newname = (count($scandir-2)) . $ext;
move_uploaded_file($_FILES["file"]["tmp_name"],"/images/news/" . $newname);
$ulink = "/images/news/" . $newname;
$result = "Success, please copy your link below";
}
}
else
{
$result = "Error.";
}
}
When I upload a .png image, the page simply seems to refresh, I've placed the echo "Found."; in there to check if it even has anything in $_POST["file"] but it doesn't seem to have anything.
I don't understand why the page isn't submitting correctly. I've changed action="" to action="upload.php" to make sure it points to the same page but still nothing.
Use $_FILES['file'] instead of $_POST['file'].
Read more about $_FILES at http://www.php.net/manual/en/features.file-upload.post-method.php
replace $_POST['file'] by $_FILES['file'] and set action="".
Try this.... because $_POST not work with files, for files we use $_FILES..
if(!empty($_FILES['file']))
{
echo "Found.";
$exts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$ext = end($temp);
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"] < 20000)
&& in_array($ext, $exts))
{
if($_FILES["file"]["error"] > 0)
{
$result = "Error Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
$scandir = scandir("/images/news/");
$newname = (count($scandir-2)) . $ext;
move_uploaded_file($_FILES["file"]["tmp_name"],"/images/news/" . $newname);
$ulink = "/images/news/" . $newname;
$result = "Success, please copy your link below";
}
}
else
{
$result = "Error.";
}
}
I wouldn't just check the $_FILES variable. I would name the submit input and check if the submit input was submitted. This way you can check if the button was pressed with no files selected and prompt the user as such.
Like So:
<form action="" method="post" enctype="multipart/form-data">
<ul>
<li>
<label for="file">File : </label>
<input type="file" id="file" name="file" required="required" />
</li>
<li>
<input type="submit" value="Upload" name="upload"/>
</li>
</ul>
</form>
Then you can check the post variable for that value.
Like So:
if(!empty($_POST['upload']))
{
echo "Found.";
$exts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$ext = end($temp);
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"] < 20000)
&& in_array($ext, $exts))
{
if($_FILES["file"]["error"] > 0)
{
$result = "Error Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
$scandir = scandir("/images/news/");
$newname = (count($scandir-2)) . $ext;
move_uploaded_file($_FILES["file"]["tmp_name"],"/images/news/" . $newname);
$ulink = "/images/news/" . $newname;
$result = "Success, please copy your link below";
}
}
else
{
$result = "Error.";
}
}

PHP - Create a folder and save an image to that folder? Then display that photo?

This seems like a big question, and it is, but the majority is done but I'm lost in actually getting where I want to be.
I have the upload form ready and it works, it saves it to a folder named upload, but I need it to create a folder depending on the hotel name that is saved in my session. This is displayed on the page to tell users what session they are on. So now when they save I need it to create the folder named after it to save it to that directory. So here is the code for the HTML :
<span class="prettyFile">
<form id="form" action="assets/php/upload.php" method="post" enctype="multipart/form-data">
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="input-append">
<div class="uneditable-input span3" style="margin-top: -3px;"><i class="icon-file fileupload-exists"></i> <span class="fileupload-preview"></span></div><span class="btn btn-file btn-success"><span class="fileupload-new">Upload File</span><span class="fileupload-exists">Change</span><input type="file" name="file" /></span>Remove
<button type="submit" name="submit" class="btn btn-primary fileupload-exists" value="Upload"><i class="icon-cloud-upload"></i> Upload</button>
</div>
</div>
</form>
</span>
And here is the PHP:
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
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"] < 10000000)
&& in_array($extension, $allowedExts))
if (file_exists("upload/". $_SESSION['curHotelName'] . "/" . $_FILES["file"]["name"]))
{
echo "<script language='javascript'>\n";
echo "alert('This file exists! Please select a different file.'); window.location.href='https://hotelmobidev.jvgames.com/profile';";
echo "</script>\n";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/". $_SESSION['curHotelName'] . "/" . $_FILES["file"]["name"]);
echo "<script language='javascript'>\n";
echo "alert('Success!'); window.location.href='https://hotelmobidev.jvgames.com/profile';";
echo "</script>\n";
}
?>
I'm still messing with it but if someone can bump me in the right direction that would be awesome! Thanks guys!
You need to create the directory before you can move files into it. You can use the file_exists function to check on the directory first and then create it of needed:
if (!file_exists('path/to/directory')) {
mkdir('path/to/directory', 0777, true);
}
You may prefer other permissions.
Ajax can help you ;)
Make one script - for uploading
Second - for check file in the folder & if it exists - display it
p.s. you will not resolve this problem with php only
sorry for bad engl

Error while retrieving files from server

<?php
if(isset($_POST['pic'])){
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 300000))
{
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 "<h1>Done! Looks Great!</h1>";
}
}
else
{
echo "Invalid file";
}
}
?>
<form action="editprofile.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<p>Image format should be png or jpg.</p>
<center><p class="submit"><input type="submit" name="pic" value="Upload Picture" /></p></center>
</form>
</div>
<p style="text-align:center; font-size:18px;">Current Picture</p>
<?php
$filename = $_FILES['file']['tmp_name'];
?>
<img src="/path/to/the/upload/folder/<?php echo $filename; ?>"/>
<img src="../../upload/foto.PNG" class="picture"/>
I am getting an error like undefined index - file.
Error is in the last few lines.
I basically have a folder which has an image. I want it to display the only image in the folder.
If you open the page for the first time, no form has been sent yet and $_FILES is therefore empty. You try to access $_FILES even in case of first load. This is the faulty line:
$filename = $_FILES['file']['tmp_name'];
You should check that $_POST["pic"] is set before accessing the $_FILES variable (just as you have done on the top of the code).

Categories