Downloading image from POST
Posted: Fri Jul 08, 2011 12:10 pm
Hi,
I'm an iPhone/Android programmer who just recently started using PHP for a project. The app I'm designing sends images over HTTP POST via the iPhone. Currently my server will download and store about 19 out of 20 pictures, however I'm trying to get the most reliability I can, I'd prefer to only see one dud picture out of a few hundred pictures, instead every 20. Is there a reason why sometimes I get these dud pictures? I asked on the iPhone developer forums and they said it wasn't my Objective C programming and that the fault either was the server or the php code. Here's the php code that the iPhone NSUrlConnection sends the photos too,
Now, the dud pictures do store information in the database, so I know that the iPhone is making the connection and the php file is called. However, the picture doesn't download to the uploads folder. So when the site loads the photos it just shows an empty picture for the dud. It might also help to know that when I moved this to another server it improved greatly (from about 3 duds every 20 pictures to 1 dud every 20 pictures).
Is there a more reliable way to handle this? Thanks and let me know if you have any further questions!
-Christopher Nuland (3iD Software Engineer)
I'm an iPhone/Android programmer who just recently started using PHP for a project. The app I'm designing sends images over HTTP POST via the iPhone. Currently my server will download and store about 19 out of 20 pictures, however I'm trying to get the most reliability I can, I'd prefer to only see one dud picture out of a few hundred pictures, instead every 20. Is there a reason why sometimes I get these dud pictures? I asked on the iPhone developer forums and they said it wasn't my Objective C programming and that the fault either was the server or the php code. Here's the php code that the iPhone NSUrlConnection sends the photos too,
Code: Select all
<?php
require_once('Includes/connection.php');
$uploaddir = './uploads/';
$file = basename($_FILES['userfile']['name']);
$image = $_FILES['userfile']['name'];
$info = getImageSize($_FILES['userfile']['tmp_name']);
$name = basename($_FILES['fileName']['name']);
//$date = basename($_FILES['fileDesc']['name']);
$desc = basename($_FILES['fileDesc']['name']);
$picID = basename($_FILES['fileID']['name']);
$gps = basename($_FILES['gps']['name']);
$uploadfile = $uploaddir . $file;
$myFile = "testFile.txt";
$fh = fopen($picID, 'w') or die("can't open file");
$stringData = "{}\n";
fwrite($fh, $stringData);
//$stringData = "{$date}\n";
//fwrite($fh, $stringData);
//////////////////////////////////////////////////
$picQuery=mysql_query("select newPics from members where username = '$picID'");
$picCount = mysql_fetch_array($picQuery);
$newpics =(int)$picCount["newPics"] + 1;
echo $newpics;
mysql_query("UPDATE members SET newPics = '$newpics' WHERE username = '$picID'");
$uploadedfile = ($_FILES['userfile']['tmp_name']);
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
if ($width > $height)
{
$newwidth=150;
$newheight=113;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$newwidth1=150;
$newheight1=113;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
}
else
{
$newwidth=113;
$newheight=150;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$newwidth1=113;
$newheight1=150;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
}
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,
$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,
$width,$height);
$myquery=mysql_query("select max(id) as maxnum from images");
$myrow = mysql_fetch_array($myquery);
$myname =(int)$myrow["maxnum"] + 1;
$image_name=$file;
$newname = "uploads/". $myname.".jpg";
$newname1 = "uploads/thumbs/". $myname.".jpg";
imagejpeg($tmp,$newname,100);
imagejpeg($tmp1,$newname1,100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
$copied = copy($_FILES['userfile']['tmp_name'], $newname);
$date = date("F j, Y, g:i a");
//////////////////////////////////////////////////////////////////////
$query = sprintf("insert into images (filename, mime_type, file_size, project_name, image_desc, pictureID, date, gps)
values ('%s', '%s', %d, '$name', '$desc', '$picID', '$date', '$gps')",
mysql_real_escape_string($_FILES['userfile']['name']),
mysql_real_escape_string($info['mime']),
$_FILES['userfile']['size'],
mysql_real_escape_string(
// file_get_contents($_FILES['userfile']['tmp_name'])
)
);
mysql_query($query, $connection);
$id = (int) mysql_insert_id($connection);
$stringData = "works\n";
fwrite($fh, $stringData);
fclose($fh);
?>Is there a more reliable way to handle this? Thanks and let me know if you have any further questions!
-Christopher Nuland (3iD Software Engineer)