I have a Multi Step form with 6 pages. Each page has 2-3 inputs. There are next and previous buttons on each page. Whenever user fills data in one of the page and submits, a php file is called through ajax call. Validation for all the steps are done in that same file. If all the inputs have correct data then next page is shown. All the steps are defined by their step no.
On the second last step there is a file input that takes an image. Sends it through ajax and stores in files. I have to store it in files somewhere to be able to show to the user.
Now on last step all the data has to be shown for last verification. Everything the user has entered is shown to him. Where he chooses to dismiss all the data or store it in the profile.
What if he does nothing and closes the tab. The Session variables will get destroyed but the image file he uploaded will be stuck in the files forever.
So, my question is how can i store that image in such a way so that whatever activity user does except (to dismiss or to accept), the image gets removed from the files.
A wild guess : can i store it in the sessions? Or cookies?
** Here Is The Code For Upload Verification **
/* Page 5 */
if($page == $page5)
{
if(isset($_FILES['uploadedVisitingCard']))
{
$image = new image;
$outImage;
try{
if(!list($width, $height, $type) = getimagesize($_FILES['uploadedVisitingCard']['tmp_name']))
{
throw new Exception("Either Uploaded File Is Not An Image or It An Image Type That Is Not Supported");
}
$image->type = $type;
$image->width = $width;
$image->height = $height;
}
catch(Exception $e)
{
$result['error'] = true;
$result['msg'] = $e->getMessage();
echo json_encode($result);
die();
}
if(!in_array($image->type, $allowedImageType))
{
$result['error'] = true;
$result['msg'] = "File Type Not Supported For Uploading! The File Must Be a PNG File or a JPG File";
echo json_encode($result);
die();
}
try{
if(!$image->size = filesize($_FILES['uploadedVisitingCard']['tmp_name']))
{
throw new Exception("Unable To Get The Size OF The Uploaded Image.");
}
}
catch(Exception $e){
$result['error'] = true;
$result['msg'] = $e->getMessage();
echo json_encode($result);
die();
}
if($image->size > 5242880)
{
$result['error'] = true;
$result['msg'] = "The Image Must Be Below 5MB";
echo json_encode($result);
die();
}
try{
switch ($image->type)
{
case IMAGETYPE_JPEG:
if(!$outImage = imagecreatefromjpeg($_FILES['uploadedVisitingCard']['tmp_name']))
{
throw new Exception("Cannot Reproduce The JPEG Image! Error Code: IMGDUPJPEG");
}
if(!$outImage = resize_image($outImage, $image, 640, 480))
{
throw new Exception("Cannot Reproduce The JPEG Image! Error Code: IMGDUPJPEGFUNC");
}
if(!imagejpeg($outImage, "main.jpg", 70))
{
throw new Exception("Cannot Save The File Into The Servers! Error Code: IMGSAVJPEG");
}
$result['error'] = true;
$result['msg'] = "Image Is JPEG";
echo json_encode($result);
die();
break;
case IMAGETYPE_PNG:
if(!$outImage = imagecreatefrompng($_FILES['uploadedVisitingCard']['tmp_name']))
{
throw new Exception("Cannot Reproduce The JPEG Image! Error Code: IMGDUPPNG");
}
if(!$outImage = resize_image($outImage, $image, 800, 600))
{
throw new Exception("Cannot Reproduce The JPEG Image! Error Code: IMGDUPPNGFUNC");
}
if(!imagepng($outImage, "main.png", 9))
{
throw new Exception("Cannot Save The File Into The Servers! Error Code: IMGSAVPNG");
}
$result['error'] = true;
$result['msg'] = "Image Is PNG";
echo json_encode($result);
die();
break;
}
}
catch(Exception $e)
{
$result['error'] = true;
$result['msg'] = $e->getMessage();
echo json_encode($result);
die();
}
$result['error'] = true;
$result['msg'] = "Width = ".$image->width." \nHeight = ".$image->height." \nImage Type = ".$image->type;
echo json_encode($result);
die();
}
else
{
$result['error'] = true;
$result['msg'] = "The Image Was NOt Uploaded";
echo json_encode($result);
die();
}
}
** Here Is The Function That Fetches Image Details, Resize Them And Renders Out Image In The File **
function resize_image($file, image $image, $dstWidth, $dstHeight)
{
$dst; $newwidth; $newheight;
$r = $image->width / $image->height;
if ($dstWidth/$dstHeight > $r) { $newwidth = $dstHeight*$r; $newheight = $dstHeight; }
else { $newheight = $dstWidth/$r; $newwidth = $dstWidth; }
switch($image->type)
{
case IMAGETYPE_JPEG:
try{
if(!$dst = imagecreatetruecolor($newwidth, $newheight)){
throw new Exception("Problem In Duplication Of The File! Error Code: IMGDUPJPEGFUNC1");
}
if(!imagecopyresampled($dst, $file, 0, 0, 0, 0, $newwidth, $newheight, $image->width, $image->height)){
throw new Exception("Problem In Duplication Of The File! Error Code: IMGDUPJPEGFUNC2");
}
}
catch(Execption $e)
{
$result['error'] = true;
$result['msg'] = $e->getMessage();
echo json_encode($result);
die();
}
break;
case IMAGETYPE_PNG:
try{
if(!$dst = imagecreatetruecolor($newwidth, $newheight)){
throw new Exception("Problem In Duplication Of The File! Error Code: IMGDUPPNGFUNC1");
}
if(!imagecopyresampled($dst, $file, 0, 0, 0, 0, $newwidth, $newheight, $image->width, $image->height)){
throw new Exception("Problem In Duplication Of The File! Error Code: IMGDUPPNGFUNC2");
}
}
catch(Execption $e)
{
$result['error'] = true;
$result['msg'] = $e->getMessage();
echo json_encode($result);
die();
}
break;
}
return $dst;
}
function process_image($file)
{
global $allowedImageType;
$image = new Image;
try{
if(!list($width, $height, $type) = getimagesize($file))
{
throw new Exception("Either Uploaded File Is Not An Image or It An Image Type That Is Not Supported");
}
$image->type = $type;
$image->width = $width;
$image->height = $height;
}
catch(Exception $e)
{
$result['error'] = true;
$result['msg'] = $e->getMessage();
echo json_encode($result);
die();
}
if(!in_array($image->type, $allowedImageType))
{
$result['error'] = true;
$result['msg'] = "File Type Not Supported For Uploading! The File Must Be a PNG File or a JPG File";
echo json_encode($result);
die();
}
try{
if(!$image->size = filesize($file))
{
throw new Exception("Unable To Get The Size OF The Uploaded Image.");
}
}
catch(Exception $e){
$result['error'] = true;
$result['msg'] = $e->getMessage();
echo json_encode($result);
die();
}
if($image->size > 5242880)
{
$result['error'] = true;
$result['msg'] = "The Images Must Be Below 5MB";
echo json_encode($result);
die();
}
return $image;
}
function render_image($image, $path)
{
try{
switch ($image->type)
{
case IMAGETYPE_JPEG:
if(!$outImage = imagecreatefromjpeg($_FILES['uploadedVisitingCard']['tmp_name']))
{
throw new Exception("Cannot Reproduce The JPEG Image! Error Code: IMGDUPJPEG");
}
if(!$outImage = resize_image($outImage, $image, 640, 480))
{
throw new Exception("Cannot Reproduce The JPEG Image! Error Code: IMGDUPJPEGFUNC");
}
if(!imagejpeg($outImage, "main.jpg", 70))
{
throw new Exception("Cannot Save The File Into The Servers! Error Code: IMGSAVJPEG");
}
$result['error'] = true;
$result['msg'] = "Image Is JPEG";
echo json_encode($result);
die();
break;
case IMAGETYPE_PNG:
if(!$outImage = imagecreatefrompng($_FILES['uploadedVisitingCard']['tmp_name']))
{
throw new Exception("Cannot Reproduce The JPEG Image! Error Code: IMGDUPPNG");
}
if(!$outImage = resize_image($outImage, $image, 800, 600))
{
throw new Exception("Cannot Reproduce The JPEG Image! Error Code: IMGDUPPNGFUNC");
}
if(!imagepng($outImage, "main.png", 9))
{
throw new Exception("Cannot Save The File Into The Servers! Error Code: IMGSAVPNG");
}
$result['error'] = true;
$result['msg'] = "Image Is PNG";
echo json_encode($result);
die();
break;
}
}
catch(Exception $e)
{
$result['error'] = true;
$result['msg'] = $e->getMessage();
echo json_encode($result);
die();
}
}
The easiest way is to create an actual file management system. Meaning, you have a database table somewhere in which you store information about the files. The actual files should be stored in a folder structure somewhere on disk, with randomly generated names. UUIDs are very useful for this purpose. In the database then you store that UUID/file path, and any additional information about the file you want; e.g. its original user-supplied name, when it was uploaded, by whom it was uploaded etc.
Having this information allows you to act on it. E.g., you can query for all files which have been uploaded more than 24 hours ago, but have not been used anywhere else after that, so which can be deleted. Or perhaps you set a
temporary = true
flag when you upload them, and at the end of your wizard you remove thetemporary
flag, and you delete alltemporary
files after 24 hours. This can all be done with a simple, regular cron job.In other words: you don't treat the files any differently from any other uploaded files, you merely retain enough information about them somewhere that allows you to remove them later as necessary.