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."
Related
my upload.php is only uploading a png image type.In my code i've allowed extensions of (jpg,png,jpeg,gif) to be uploaded.I expect that i am able to upload the image stated without issues.
In my code i think there is an issue with $_FILES variable.I've tried to print to the screen the $_FILES variable and it displays the image file information of a png image,but fails to print to the screen the image information of an image with different extension.
<?php
if(isset($_POST['submitpics'])){
$file=$_FILES['upfile'];
printr($file); exit();
$filename=$_FILES['upfile']['name'];
$filetype=$_FILES['upfile']['type'];
$fileerror=$_FILES['upfile']['error'];
$filesize=$_FILES['upfile']['size'];
$filetmp_name=$_FILES['upfile']['tmp_name'];
$fileext=explode(".", $filename);
$fileactualext=strtolower(end($fileext));
$allowed=array( jpg, jpeg, gif, png); //allowed extensions
if(in_array($fileactualext,$allowed)){
if($fileerror == 0){
if($filesize < 4000000){
$filenamenew="profile".$id.".".$fileactualext;
$filedestination="uploads/".$filenamenew;
move_uploaded_file($filetmp_name, $filedestination);
$sql="UPDATE profileimg SET status = 0 WHERE userid='$id';";
mysqli_query($conn,$sql);
header("Location: index.php");
} else{
echo "<strong>File too large</strong>";
exit();
}
} else {
echo "<strong>An error occured</strong>";
}
} else {
echo "<h2>This file is not allowed!</h2>";
}
}
according to the code above,i only printed $_FILES variable to the screen to find out if the image information was actually collected by the super global variable($_FILES).And when i uploaded png image ,it worked but when i uploaded jpg image or any other extension,it fails.
And when i upload an image that isn't png without this line of code(printr($file); exit(); ) ,i get an error message from google saying "The site can't be reached".
generally all i need is to be able to upload the images i've specified.
And i don't know where error is in my code.I need some help please!!
welcome to stackoverflow.
I can see two problems in your post. The first one is that this line
$allowed=array( jpg, jpeg, gif, png); //allowed extensions
should be
$allowed=array( "jpg", "jpeg", "gif", "png"); //allowed extensions
because you want to match strings (text). The other thing is the error you are getting: i get an error message from google saying "The site can't be reached"
Seems that, for some reason, you are redirecting to a domain that is not available, probably not your site. It's strange that this happens only when you upload a file that is not a png, but maybe you have some javascript code doing something else in your frontend.
You can update your question with the webpage you use to post to your backend code if you need further help.
Let me recommend a debug tool to make development easy: https://xdebug.org/ you can put breakpoints in your code and run it line per line to understand better what's going on. You can use xdebug with almost any known IDE to make it simple to debug.
Good luck
instead of explode , use pathinfo to get correct extension
$filename=$_FILES['upfile']['name'];
$fileext = pathinfo($filename, PATHINFO_EXTENSION);
more details about pathinfo pathinfo()
$info = new SplFileInfo($_FILES['upfile']['name']);
$extension=$info->getExtension();
if($extension=='png')
{
DoUpload();
}
Also u can get size of file with Splfileinfo class or more data
$size=$info->getSize();
For get type of file
$fileType=$info->getType();
I am trying to upload an image to server (I am using Google Cloud Virtual Machine) that the PHP gets from a Python script (I intend to retrieve the images from a Raspberry Pi camera sensor, but for now, I am sending a placeholder image to test the functionality of my script) that sends the image along with other parameters through a POST request. However, while the image does get uploaded, it does not get moved to a new location as it is shown in the Output I have provided below along with the code.
A question found here: php uploading a file says it is working but does not actually upload a file does have a similar problem, however the suggested solution there did not help me.
Here's the code that implements the file upload system.
PHP Upload File code:
//Upload the image
$uploadDir = "/home/username/directory/";
$uploadFile = $uploadDir . basename($_FILES["myimage"]["name"]);
if(is_uploaded_file["myimage"]["tmp_name"])
{
echo "File has been UPLOADED\n";
if(move_uploaded_file($_FILES["myimage"]["tmp_name"],$uploadFile))
{
echo "File has been MOVED";
}
else
{
echo "File has NOT been MOVED";
}
}
else
{
echo "File has NOT been UPLOADED";
}
Python code which sends a request to the PHP above:
import requests
def postData(directory):
activity = "Someone walked in"
url = 'http://35.198.114.146/index.php/sensor_readings/postdata?activity=' + activity
image = {'myimage': open(imagePath,'rb')}
r = requests.post(url,files=image)
print(r.text)
imagePath = 'cam_images/test.png'
postData(imagePath)
Output:
File has been UPLOADED
File has NOT been MOVED
I have been stuck on this problem for weeks and I am not sure on what to do. Any kind of help is appreciated. Should you need more details or code, feel free to ask me.
Thank you in advance.
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.
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'.
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
}