У меня есть многошаговая форма с 6 страницами. Каждая страница имеет 2-3 входа. На каждой странице есть кнопки «Следующая» и «Предыдущая». Всякий раз, когда пользователь заполняет данные на одной из страниц и отправляет, php-файл вызывается через вызов ajax. Проверка всех шагов выполняется в том же файле. Если все входные данные имеют правильные данные, отображается следующая страница. Все шаги определяются их шагом №.
На втором последнем шаге есть файл ввода, который принимает изображение. Отправляет его через ajax и сохраняет в файлах. Я должен хранить его в файлах где-то, чтобы иметь возможность показать пользователю.
Теперь на последнем шаге все данные должны быть показаны для последней проверки. Все, что пользователь ввел, показывается ему. Где он решает отклонить все данные или сохранить их в профиле.
Что делать, если он ничего не делает и закрывает вкладку. Переменные Session будут уничтожены, но загруженный им файл изображения будет навсегда застрял в файлах
Итак, мой вопрос: как я могу сохранить это изображение таким образом, чтобы независимо от действий пользователя, кроме (отклонить или принять), изображение было удалено из файлов.
Дикая догадка: могу ли я сохранить это в сессиях? Или печенье?
** Вот код подтверждения загрузки **
/* 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();
}
}
** Вот функция, которая выбирает детали изображения, изменяет их размер и отображает изображение в файле **
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();
}
}