image upload changes image lot of time after form submit - php

im having some trouble. I have the following script for uploading a profile image, The first time the user uploads, it works fine, the second time it does no change, you select the image, submit the form, and the same image shows as profile, not the new. And i just realized that after a couple of hours, you enter to the site and the image has changed, what can this be? WHy is it that the image is not changing when submiting like in my localhost?
HERES THE CODE:
if (isset($_POST['parse_var'])){
if ($_POST['parse_var'] == "pic"){
// If a file is posted with the form
if ($_FILES['fileField']['tmp_name'] != "") {
$maxfilesize = 4109212;
$target_dir = "users/$logOptions_id/profile.jpg";
if($_FILES['fileField']['size'] > $maxfilesize ) {
$error_msg = '<font color="#FF0000">ERROR: Your image was too large, please try again.</font>';
unlink($_FILES['fileField']['tmp_name']);
} else if (!preg_match("/\.(gif|jpg|png|jpeg)$/i", $_FILES['fileField']['name'] ) ) {
$error_msg = '<font color="#FF0000">ERROR: Your image was not one of the accepted formats, please try again.</font>';
unlink($_FILES['fileField']['tmp_name']);
} else {
$newname = "profile.jpg";
$place_file = move_uploaded_file($_FILES['fileField']['tmp_name'], "users/12/".$newname);
}
}
}
}
Thanks in advanced, I've been with this issue for 2 days now and i have no idea how to fix it.

i found the solution, it was that the images where being cached, so the browser was showing the cached images. The solution was to ad ? and a rand number after the .jpg at the image src.

What are the errors? knowing the error might help to solve the problem. And check your folder permissions too. These kind of errors on live servers happen because the folders do not have enough write permissions.
Goto your cpanel or whatever you have to manage your server, check the if your users folder has write permissions.

Related

HTML loaded image goes away after refresh

So this is a weird phenomenon I've never experienced in that I have an HTML form on image upload (exactly the one on W3). It does it's job properly when clicked and pushes the user profile picture into a folder while saving the name of the file on a database. Underneath the upload, I have a little image tag that spits out the uploaded profile picture. The end goal is for that profile picture to be displayed on the page constantly while the session exists. When uploaded, it works perfectly fine and the profile picture appears. After one refresh, the image can't be found and the alt="blank" takes over. The location still stays proper in the database so I don't think that's the issue. Is there an error? Do I need to use JS onload? Does the image tag only work once? Please help and thank you for taking the time to read this.
PHP:
echo '<img src="'.$loaded_profile_picture.'"id="HOMEPROFILE" alt="blank" style="width:128px;height:128px">';
//$loaded_profile_picture has the value uploads/photo.jpg
Classes.php:
public function addProfilePicture(){
include_once "conn.php";
$sql=$dbh->prepare("UPDATE users SET UserProfilePicture=:UserProfilePicture WHERE UserName=:UserName;");
$sql->execute(array(
'UserProfilePicture'=>$this->getUserProfilePicture(),
'UserName'=>$_SESSION['UserName'],
));
}
Image Upload (Might be the culprit):
if($uploadsuccess&&move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file)){
echo " The file ".basename($_FILES['fileToUpload']['name'])." has been uploaded.";
$user->setUserProfilePicture($target_file);
$user->addProfilePicture();
if($user->getUserProfilePicture()!=NULL){
echo '<img src="'.$target_file. '" alt="nom">';
}
}
Edit:
While many users have a similar problem (i.e. Image display with <img src=""> not working) I have tried looking at localhost:8888/xampp/LocalPost/pages/uploads/photos.jpg and it displays just fine. I have also changed file permissions on Windows 7 with no luck. Thanks for all your hard work I'll keep searching.
Edit:
I got around the problem by using $_SESSION['UserProfilePicture'] and it sticks after a refresh. This doesn't solve the question. I'd still very much like someone to help me find what went wrong, because later on I would like to display other people's Profile Pictures which you obviously can't do with session variables.
first of all your missing } et the end of your script
if($uploadsuccess && move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file)){
echo " The file ".basename($_FILES['fileToUpload']['name'])." has been uploaded.";
$user->setUserProfilePicture($target_file);
$user->addProfilePicture();
if($user->getUserProfilePicture()!=NULL){
echo '<img src="'.$target_file. '" alt="nom">';
}
}
Check once that after refreshing your page you will be get the value of $loaded_profile_picture variable or not?
If you got the value of $loaded_profile_picture variable then check it is same as your previous path or not?

php website mobile version, image shows sideways when uploaded

I am using this code to upload a picture from my mobile on my php website mobile version. The problem is for a strange reason the image get rotated. I need a solution as I have been trying in vain till now.
The code I am using for my php site is :
<?php
if (isset($_FILES["pics"]["name"]) )
{
$errors= array();
foreach($_FILES['pics']['tmp_name'] as $key => $tmp_name )
{
$file_name = $key.$_FILES['pics']['name'][$key];
$file_size =$_FILES['pics']['size'][$key];
$file_tmp =$_FILES['pics']['tmp_name'][$key];
$file_type=$_FILES['pics']['type'][$key];
$file_name1[] = $file_name;
$desired_dir="users/timeline/".$_SESSION['session_uid'];
if(empty($errors)==true)
{
if(is_dir($desired_dir)==false)
{
mkdir("$desired_dir", 0777); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false)
{
move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
}
else
{ // rename the file if another one exist
$new_dir="$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
}
}
}
?>
HELP PLEASE :)
Ok, did some Googling. Your code is fine, the image is rotated before it is upload. I am assuming you are using an iPhone so here is a great resource to help you with your problem:
http://iphonephotographyschool.com/iphone-photos-upside-down/
From the link above:
"Your iPhone doesn’t convert any images to correct orientation because that would keep it busy for a few seconds. Instead it simply saves all photos as they were recorded and adds information about their correct orientation to EXIF tags. This means that your iPhone is ready to shoot another photo within a fraction of a second, which is just awesome."

File uploading only working with some images (class.upload.php)

I've tried the native way of uploading images with PHP, but my friend suggested me to the class.upload.php library, which still had the same results as before. I can only upload certain images without them having that little icon that means that the browser can't find the image, but what's weird is that when I download the "invalid" images to my computer, they're fine. About 50% of images actually do work, but the rest just show the appropriate size that they should have but no image. Here is my code (my html form has a file input type called filename:
$handle = new upload($_FILES['filename']);
if ($handle->uploaded)
{
if ($file_src_size < 20000)
{
$handle->file_new_name_body = "test";
$handle->image_convert = 'jpg';
$handle->process('/');
echo "<img src = \"test.jpg\" />";
}
else echo "Files must be 20 kb or under";
}
else echo "Upload failed, please try again";
The fact that you can download the images and re-display locally them suggests that the problem is not with the upload script. An alternative problem could be that the images are not properly saved for web viewing. For example, just because a file ends in .jpg and can be viewed locally does not mean that it is recognizable by a browser. A CMYK jpeg cannot be viewed by many browsers.
You need to debug your code as something is going wrong, and only someone with access to your computer can tell you why.
1) The library you're using has error detection code
if ($handle->processed) {
echo 'image resized';
$handle->clean();
} else {
echo 'error : ' . $handle->error;
}
2) You should be able to look at the processed image data, and figure out why the browser is not able to parse it as an image. If you can give a URL for an invalid image I would be able to tell you what's wrong with it.
3) Writing files to the root directory by calling "process('/');" is bad. You should make a directory to hold files that come from other people, to avoid having potential problems when some uploads an image called 'index.php'.

PHP Upload file-type and -size restrictions

ok, so I made this and this works just fine and well:
$file_name = $HTTP_POST_FILES['ufile1']['name'];
$file_name_for_db = ($HTTP_POST_FILES['ufile1']['name']);
$new_file_name = $id."_pic1_".$file_name;
$path = "../users/".$username_session."/photos/".$new_file_name;
if($ufile !=none)
{
copy($HTTP_POST_FILES['ufile1']['tmp_name'], $path);
$path_of_pic = "../users/".$username_session."/photos/".$id."_pic1_".$file_name_for_db;
mysql_query("UPDATE pictures SET pic_1 = '".$path_of_pic."' WHERE id = '$id'");
header("location:../upload/");
}
else
{
echo "Error";
}
Now what I've been trying to do is add restrictions.
I've looked through many restriction tutorials, they don't work, always returns error no matter what.
I just want to restric from users uploading anything other than pictures (.png, .jpg, .jpeg, .gif, .bmp) and no more than 5mb
I can do this in HTML, but you can easily get past that. I'm pretty new to programming, so I apologize if the way I write my codes is poor and bulky.
This site has helped me a whole bunch a few times! You guys are awesome! thanks in advance.
this link will help you to restrict images other then .jpg, .jpeg, and so on also the maximum upload size, and if you want to resize the image then also follow the tutorial : http://www.9lessons.info/2009/03/upload-and-resize-image-with-php.html
if( !#imagecreatefromstring(file_get_contents($path))) {
// (most likely) not an image
}
And:
if( filesize($path) > 5<<20) {
// image is bigger than 5MB
}

File permission to PHP uploaded image

In my PHP site I have a script to upload images, see the script below
$uploaddir = $root_path."images/uploaded_images/category/";
$small_file_name = trim($_FILES['cat_image1']['name']);
$small_file_len = strlen($small_file_name);
$small_file_ext = strtolower(substr($small_file_name,-4)); // select last 4 characters
$small_uploadfile = $uploaddir. $_FILES['cat_image1']['name'];
if($small_file_len>4 and ($small_file_ext==".gif" or $small_file_ext==".jpg" or $small_file_ext=="jpeg")){
if ($small_file_ext=="jpeg")
$uniqname = uniqid(rand()).".".$small_file_ext;
else
$uniqname = uniqid(rand()).$small_file_ext;
$thumb_filename1 = "thumb_".$uniqname; // store uniqname into database
$uploadfile = $uploaddir.$uniqname; //uncomment for local testing
if (move_uploaded_file($_FILES['cat_image1']['tmp_name'], $uploadfile)) {
chmod($uploadfile,0777);
list($width, $height, $type, $attr) = getimagesize($uploadfile);
$max_width = 276;
$max_height = 162;
$a = new Thumbnail($uploadfile,$max_width,$max_height,$uploaddir.$thumb_filename1,100,'');
$a->create();
}else{
$flag=1;
$frm_server_side_error= $frm_server_side_error."Error in uploading image,";
}
}
else{
$flag=2;
$frm_server_side_error= $frm_server_side_error."Image not in gif or jpg format,";
}
After uploading in to the server its can't be see (I am displaying the thumb image). So I checked the file permission of that image through the FTP and give read permission. Thus image displays. But I give the read and write permission to the category folder and it’s parent folders. How can view the image after uploading (default read permission to the uploading files ie thumb image.)
I noticed that the actual image have read permission but the thumb image doesn't have the read permission. I need to display only the thumb image.
You have used a class Thumbnail to create a thumbnail image. But I guess you have missed to change the file permission in the new thumbnail image. Please check the code in the create() function of the class Thumbnail
Your code formatted poorly here (half is in code format, half not), but it appears you have a comment before the snippet that would copy the file from the temp location to the final location - if that's the case, the file won't exist, and thus won't be viewable.
But I could be wrong - I'm happy to take a second look if you fix the formatting.

Categories