Correct permissions to access the file location of image - php

I have this code where I can upload images. Previously, I'm having trouble changing the path/directory of the folder. And its working fine now. I have the correct path inside my database and also the image is being saved into the correct folder.
My coding :
<table id="details" height="100">
<tr>
<td>Select Image </td>
<td> : </td>
<td><input type="file" name="image" class="ed"></td>
</tr>
</table>
if (!isset($_FILES['image']['tmp_name']))
{
echo "";
}
else
{
$file=$_FILES['image']['tmp_name'];
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image']['name']);
$location= $_SERVER['DOCUMENT_ROOT'] . '/ehars/photo/';
move_uploaded_file($_FILES["image"]["tmp_name"], $location . $_FILES["image"]["name"]);
$save=mysql_query("INSERT INTO photo (location,emp_id) VALUES ('$location','$emp_id')");
}
Path in my db :
Image saved in the folder :
However, when I try to view the picture inside the folder, I cannot view it. It says like in the photo below. Is there something wrong with my code? Or do I need to enable something so that I can view my photo? Thank you.

Try uploading a .jpg file instead of a .png. Or using another photo viewer program.
There is/was a problem with Photo Viewer not loading .png files
See this for a possible solution

Based on the discussion in the comments. I am sharing the code what I use to upload an image(save to a dir).I also didn't understood why you are using addslashes(). Sorry about this!
Basically what I do is -
created dir in my project folder
Created an unique name for the file to store so that conflict does not happen
This is my html form where I get the file from the User.
<form role="form" method="post" action="save_blog.php" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Image</label>
<div class="col-sm-2">
<input type="file" class="filestyle" name="image_upload" data-input="false" id="file_name"><label id="name_of_image_file"></label>
</div>
</div>
</form>
And my save_blog.php looks like this.
$temp = explode(".",$_FILES["image_upload"]["name"]);
$unique_name_of_image = date('y-m-d') . "_".$user_id ."_".rand(1,9999)."." .end($temp);
$file_location = "$target_dir".$unique_name_of_image;
if (move_uploaded_file($_FILES["image_upload"]["tmp_name"], $file_location)) {
//this prints the location of the file stored
#echo "$target_dir".$unique_name_of_image; echo "<br>";
#$insert_blogs_with_image --> this is my insert string
save_to_database($insert_blogs_with_image);
} else {
#echo "Sorry, there was an error uploading your file.";
}
Hope this will help to solve the problem. If this didn't help then use the sample tutorial shown by the W3schools

Related

Why these line of script does not move upload image file into desire directory?

Well I am trying to upload image into the blog/upload/. and the script location is in admin_panel/add_post.php.
<?php
if(isset($_POST['save'])){
$image = $_FILES['img']['name'];
$target_dir = "../blog/upload";
$target_file = $target_dir . basename($_FILES["img"]["name"]);
move_uploaded_file($_FILES['img']['tmp_name'],$target_file);
//get rid of all database operation and connection
}
?>
<form action="add_posts.php" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="img"><span class='name'>Select Image:</span></label>
<input type="file" class="form-control" name="img" id="img"
placeholder="Image">
</div>
Here is my url:
localhost/sensive_blog/admin_panel/add_posts.php
My project folder hierarchy:
admin_panel
->otherScript.php
->somefolder
->add_post.php
blog
->someotherscripts.php
->folder
add this after $target_dir, to check if folder exist
if (!is_dir($target_dir)) {
die('path not found);
}
Finally I found my mistake. Basically it was uploading image but at wrong directory which is blog with weird name "upload[image_name]". so Then I put one forward slash / to go to right directory. and then magic did happen.

Retrieve Images From Folder with Codeigniter

My teacher,he was uploading and retrieving images from folder with ID recognition. It works with database.
So, when we upload an image, it will be stored in a folder with it's name changing automatically with ID.
Example: The upload form must be on "yourwebsite.com/yourpages/3144242" so the image will be stored as "3144242" and blablabla the same for other pages with ID
This is how he upload and retrieve it
<?php
$ff = './assets/images/kegiatan/'.$_SESSION['idkeg'].'.jpg';
if( file_exists( $ff ) ) {
<img src="<?= base_url()?>assets/images/kegiatan/<?=$_SESSION['idkeg']?>.jpg" />
<?php
}
?>
Retrieve images part:
if( file_exists( $ff ) ) {
<img src="<?= base_url()?>assets/images/kegiatan/<?=$_SESSION['idkeg']?>.jpg" />
<?php
}
?>
However, I change it a bit so I don't have to use a database and $_SESSION things. With this:
<?php
$ff = './assets/images/kegiatan/'.$this->input->post('img_name').'.jpg';
?>
<form id="form" class="kart" action="<?= base_url()?>gallery/upload2" method="post" enctype="multipart/form-data">
<div class="form-group">
<input type="text" class="form-control" name="img_name" required="required">
</div>
<div class="form-group">
<input type="file" class="form-control" name="foto">
</div>
<div class="form-group">
<button type="submit" class="btn btn-common" value="Update"><i class="fa fa-upload fa-2x"> Upload</i></button>
</div>
</form>
It works uploading images into folder "kegiatan" (means activity) with simply write the names of images in a form. The img_name that you have input, now become the name of that image.
Now I have a little problem to retrieve that images. How?
mistake in your form I realized is asking user to enter image name and get image from folder by using that input value.
First of all, I haven't tried upload an image with codeigniter but I will answer reference to php itself.
When we get an image as input from user by form, then we use "move_uploaded_file" function in php and we send temp_file, path with file name arguments to this function to save the file user selected in the form we prepared.
move_uploaded_file(string $filename , string $target);
// $filename: I use $_FILES['getFileUpload']['tmp_name'] for this argument
// $target: I define path with file name, eg: uploads/files/new_file_25.jpg for jpg image
So, you souldn't ask user to enter file name, because you will deifne it. Or, you must use "img_name" that you asked user to enter as file name which is most probably won't satisfy your ID prediction.
I hope this will help you.

upload image file to server using php

I am trying to upload selected image file into my ftp server on 1and1. I am not able to upload the file.
I have created folder called "uploadimages".
I have created a html form which has the following:
<form action="add.php" method="POST" enctype="multipart/form-data">
<div class="logo">
<label for="logoname" class="styled">Upload Logo (jpg / png):</label>
<div class="logofield">
<input type="file" id="logo" name="logo" size="30"/>
</div>
</div>
<input type="submit" value="Upload" name="submit" id="submit"/>
</form>
I have also create a php file which has the following:
<?php
$imagepic = $_FILES["logo"]["name"];
echo $imagepic;
$tempimgloc = $_FILES["logo"]["tmp_name"];
echo $tempimgloc;
$errorimg = $_FILES["logo"]["error"];
echo $errorimg;
if($errorimg > 0)
{
echo "<strong> <font size='18'>There was a problem uploading your Logo. Please try again!</font></strong>";
echo "<BR>";
}
else
{
move_uploaded_file($tempimgloc, "uploadimages/".$imagepic);
}
?>
ECHO printed results are:
1. Filename = testimage.png
2. Temp directory = /tmp/phpHvewUP
3. Error = 0
But they are not getting uploaded..
Where could I go wrong here?
Let me know!
You need to give access to your folder. Try this, chmod 777 uploadimages.
chmod 777 uploadimages
WRITING PERMISSION was the issue. Thanks Shapeshifter!

Upload File Image to server Failure

what i am trying to do is when an admin tries to create a new product to upload to the server an image of that and as it's name to be the product id.jpg. I have searched google with no result. The code seems to be right. Can someone help me please? I am using lamp as local test server.
<?php
// Parse the form data and add inventory item to the system
if (isset($_POST['product_name'])) {
$product_name = mysql_real_escape_string($_POST['product_name']);
$price = mysql_real_escape_string($_POST['price']);
$details = mysql_real_escape_string($_POST['details']);
$category = mysql_real_escape_string($_POST['category_choice']);
$condition= mysql_real_escape_string($_POST['condition']);
$supplier_choice= mysql_real_escape_string($_POST['supplier_choice']);
$handling_time= mysql_real_escape_string($_POST['handling_time']);
$weight= mysql_real_escape_string($_POST['weight']);
$information_box= $_POST['information'];
$pid = mysql_insert_id();
// Place image in the folder
$newname = "$pid.jpg";
move_uploaded_file( $_FILES['my_photo']['tmp_name'], "../inventory_images/$newname");
header("location: products.php");
exit();
}
?>
<form action="add_product.php" enctype="multipart/form-data" name="my_Form" id="my_Form" method="post">
<tr>
<td>Φωτογραφία</td>
<td>
<label>
<input type="file" name="my_photo" id="my_photo"/>
</label>
</td>
</tr>
<tr>
<td> </td>
<td>
<label>
<input type="submit" name="button" id="button" value="Αποθήκευση" />
</label>
</td>
</tr>
</table>
</form>
As you said, you are using lamp, check for the permission for the directory in which you are uploading file. In addition, also check for the permission for all the folder that you have mentioned in the destination path.
The directory and all the directory in the path, in which you are uploading file must have write permission.
If move_uploaded_file does not work then try out copy function.
Also check for the path where you are uploading is correct.
I think you'll have better luck getting answers if you thin down your code to contain only portions that are relevant to the problem. You'll probably see the answer to your own question after you do this.
For example, is the file being successfully uploaded? If not, then the mysql_xxx code and most of your form is irrelevant for your question.

Image Upload & storing the link in the database

I'm using this form to add the title,link of the image and the text of the article to the database.
I'm using type="text" for the image link,now,it's getting borring to upload an image on a external image upload service and copy the link.
I want to upload the image with this for and store THE LINK of the image in the database.
The form:
<?php if (!$_POST["go"]){ ?>
<form method="post" action="">
<input name="article_title" type="text">
<input name="article_image_url" type="text"> <!-- i want here type="file" -->
<textarea name="article_text"></textarea>
<input type="submit" name="go" value="Submit">
</form>
<?php
} else {
$date=date("Y.m.d");
$title = $_POST["article_title"];
$image_url = $_POST["article_image_url"];
$text = $_POST["text"];
$sql="INSERT INTO articles (title,image_url,text,date) VALUES ('$title', '$image_url', '$text', '$date')";
if (mysql_query($sql)){
echo "done";}
else {echo "error<br>" . mysql_error();}}
?>
Please help me with this :)
ps:sorry for my English :$
The first thing you should do, and it seems you are clueless about SQL escaping, is add following before you access the first $_POST var:
$_POST = array_map("mysql_real_escape_string", $_POST);
Then you seemingly want to use a file upload for the image. If so change the url field to:
<input type=file name=image>
This uploaded file will show up in $_FILES. Use it like this, preferrably after you've read the other fields from $_POST:
if ($img = $_FILES["image"]["tmp_name"]) {
$image_url = md5_file($img) . ".jpeg";
move_uploaded_file($img, "./upload/$image_url");
$image_url = "http://www.example.org/where/$image_url";
}
There are lot's of security concerns with that. But that's out of scope here, so I hardwired it to .jpeg. There's lots of information in the manual and its comments: http://de2.php.net/manual/en/features.file-upload.php

Categories