[nycphp-talk] opening files
Anthony Wlodarski
aw at sap8.com
Fri Oct 26 09:31:28 EDT 2007
I believe the simple way would to be to store a path in a database. Here is
an example of an upload procedure to add files to the directory (must be
world writeable though and does not provide for mime types, just extension
checks since mime_content_type() and PEAR extensions might not be an option
on shared hosting).
<?php
/*
* Created on Oct 14, 2007
*
* This application uploads a photo to be used for the profile. The image
* will be no larger than 250 x 250 pixels. If the image is not of type
gif/jpeg/png
* the user will be asked to upload a new photo that meets the requirements,
additionally
* the max file size for a picture upload is two megabytes.
*/
// pre: cookie is set for user id, source image is uploaded and mime type is
checked
// post: db is updated with link to profile photo
function buildImage($userId, $srcImage, $extension)
{
$uploadDir = './images/profilePhotos/';
// try to make sure no one has a collision of profile files
$uploadFile = $uploadDir . md5($_SESSION['id']);
// get our image dimensions so we can calculate a resize/aspect
ratio
list($width,$height) = getimagesize($srcImage);
// check what axis we are going to calculate our resize ratio from
if($width > $height)
{
$ratio = 250/$width;
$newWidth = 250;
$newHeight = $height * $ratio;
}
else
{
$ratio = 250/$height;
$newWidth = $width * $ratio;
$newHeight = 250;
}
// create an image resource from the temp uploaded file
if($extension == 'jpeg')
{
$image = imagecreatefromjpeg($srcImage);
}
elseif($extension == 'gif')
{
$image = imagecreatefromgif($srcImage);
}
elseif($extension == 'png')
{
$image = imagecreatefrompng($srcImage);
}
else
{
//output an error for extensions checks and die()
}
// create an image resource with the new dimensions
$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
// copy the image with resampling
imagecopyresampled($resizedImage, $image, 0, 0, 0, 0, $newWidth,
$newHeight, $width, $height);
// save the resized image resource to a file
// create an image resource from the temp uploaded file
if($extension == 'jpeg')
{
imagejpeg($resizedImage, $uploadFile.".".$extension, 100);
}
elseif($extension == 'gif')
{
imagegif($resizedImage, $uploadFile.".".$extension);
}
else
{
imagepng($resizedImage, $uploadFile.".".$extension);
}
// update the database with the new image link
// query data from the database for use in building the page
$dbh = mysql_connect("localhostorIPofhost", "putyourusernamehere",
"putyourpasswordhere");
// select the database
mysql_select_db("anthonyw_mystory");
$query = sprintf("UPDATE `anthonyw_mystory`.`users` SET
`profilePhoto` = '%s' WHERE `users`.`id` ='%s' LIMIT
1",$uploadFile.".".$extension,$userId);
// make change in database
$results = mysql_query($query);
// close the database connection
mysql_close($dbh);
// return them back to the lobby;
include './lobby.php';
}
// the only way we can get to this script is when someone submits
// a form submission to this script
// copy their userID
session_start();
$userId = $_SESSION['id'];
// since mime_content_type or fileinfo are not available we must
rely on the browser
// and see if it has uploaded the mime type of the file
$mimeString = $_FILES['userfile']['type'];
// now test the mime string for varying different types
if(strpos($mimeString,"jpeg") || strpos($mimeString,"jpg"))
{
$mimeType = 'jpeg';
}
elseif(strpos($mimeString,"gif"))
{
$mimeType = 'gif';
}
elseif(strpos($mimeString,"png"))
{
$mimeType = 'png';
}
else
{
$mimeType = 'invalid';
}
buildImage($userId, $_FILES['userfile']['tmp_name'], $mimeType);
?>
And this is how I pull the file out for an image wrapped in an 'img' tag:
"<img src=\"".$row['profilePhoto']."\">"
But this assumes that $row is a successful mysql_query. There are
additional functions if you know that the extension they can display images
or image resources: imagejpeg(), imagegif(), imagepng(). I did not include
the actual HTML form as I am assuming you can google that or look it up on
PHP.net
Anthony Wlodarski
aw at sap8.com
More information about the talk
mailing list