File uploading only working with some images (class.upload.php) - 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'.

Related

PHP: File is uploaded but it does not get moved from the temporary directory. How do I resolve it?

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.

Unzipped image doesn't display in browsers

fI have the following code:
$ipaFile= '/path/file.ipa';
$iconFilePath = "Payload/myapp.app/AppIcon40x40#2x.png"; // the pathway to my image file if the ipa file is unzipped.
$iconFile = "AppIcon40x40#2x.png";
$iconSaveFile = '/path/';
if ($zip->open($ipaFile) === TRUE) {
if($zip->locateName($iconFilePath) !== FALSE) {
if($iconData = $zip->getFromName($iconFilePath)) {
file_put_contents($iconSaveFile.$IconFile, $iconData);
}
}
}
This code successfully pulls an image out of an ipa (zipped) file and puts it where I want it to be. The image displays properly in image viewing programs. However, when I want to view the image in a browser, the browser tells me that the image cannot be displayed because it contains errors.
Doing further research, I get that the file is somehow not being unzipped incorrectly. There are a lot of issues regarding images not displaying in browsers, but there are a wide variety of reasons and I'm just not sure which one is mine. I've tested a variety of ways to try and fix the problem (fread method of getting file, using PHP's image functions, using headers, etc) and I can't seem to make it work. Any help would be appreciated, thanks.
For any who will wonder, when Xcode (Apple) compiles an app, it modifies the PNG files within them and a standard browser will not render them as is.
http://echoone.com/filejuicer/formats/ipa
https://theiphonewiki.com/wiki/IPA_File_Format

image upload changes image lot of time after form submit

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.

php move_uploaded_file, image turns to small red x icon when moved

I am trying to upload images and transfer them immediately to a specific folder.
Here's my code:
$file_path = "assets/placervw_photo/";
$file_path = $file_path . $_FILES['uploaded_file']['name'];
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path))
{
echo "success";
}
The page displays "success" but when I check the folder, it contains the file, having the same filename and file type, but it turns into a smal red X icon with white background. Here's a look: http://s14.postimg.org/ifoiyzde5/red_x.png
I have tested it to jpg and png files and they had the same outcome.
Any suggestions on how I can solve this? Thanks in advance! :)
The "little red X" is your browser way of saying "There's no image here".
Are you trying to display the image in the same page in which you upload it? You cannot. You already sent some content which is not image, so trying to signal the browser "I've got an image here" would get you a tiny red X.
You need to send a IMG URL with the proper URL prefix:
$url = "http://yourserver.com/path/to/".$file_path;
echo "Success, here's your image: <img src=\"$url\" />";
You'd probably also do well to not trust the user's file name, but assign a random ID of your choice, and also verify that it is indeed an image (you can use getImageSize(), also to verify the extension matches the MIME type). Just in case someone sends you a 'picture' called "../../../pwn3d.php": it's been known to happen.

Is ImageMagick identify enough to verify that the image is indeed an image

I use the code below to test whether an uploaded file is indeed an image. (The code below is not the same OOP style found in the php website when using ImageMagick because I am on a shared server and that is the instructions my hosting provided when using ImageMagick AND Actual script will involve rerouting users, unlinking or deleting the file uploaded and more so please do not criticize the code below, I just want to dwell in the concept of using IMAGICK IDENTIFY as an image verification tool.)
<?php
if(!exec('/usr/bin/identify /home/user/public_html/joteco_test_folder/thisisanimage.jpg'))
{
echo "NOT AN IMAGE";
}
else
{
echo exec('/path/here/identify /path/here/thisisanimage.jpg');
}
?>
I tried the following on the code above:
A .jpg made with photoshop cs6.
(It passed and echoed the following details "810x203 810x203+0+0 8-bit DirectClass 32.4KB 0.000u 0:00.000")
A .txt file. (It failed and echoed "NOT AN IMAGE")
A .jpg file made with notepad that has text characters written that says "I AM AN IMAGE". (It failed and echoed "NOT AN IMAGE")
(In my point of view I think it was a success, but I know that hackers do more than what I did in test #3.)
SO! Do you think that would be enough as a security check to verify if an uploaded file is indeed an image? Or are there other tools in ImageMagick that I can use for this purpose? Your thoughts?
(Pls. do not suggest or mention ( MIME | EXTENSION | GETIMAGESIZE ) as it has been repeatedly mentioned in stackoverflow as useless methods in verifying uploaded files. Thank you)
This should help you out.
$im = #imagecreatefromjpeg($imgname);
if(!$im)
{
return false;
}
else
{
imagedestroy($im);
return true;
}
In addition to checking if its an image it also returns false for incomplete (partaily uploaded) images

Categories