是否可以創建一個在開始時分配為 0 的會話變數,然后它永遠不會回傳到此代碼。我想創建某種全域迭代器,它可以在每個 php 檔案中訪問。每次上傳照片時它都會增加,但它必須在開始時有一些價值。
<?php
session_start();
$_SESSION['imageIterator'] = 0; // THIS SCRIPT IS EXECUTED EVERY TIME WHEN YOU UPLOAD FILES, SO EVERY TIME IT WILL BE ASSIGNED TO 0. I WANT IT ONLY ONCE
// Access the $_FILES global variable for this specific file being uploaded
// and create local PHP variables from the $_FILES array of information
$title = $_POST['title'];
$author = $_POST['author'];
$watermark = $_POST['watermark'];
$fileName = $_FILES["uploaded_file"]["name"]; // The file name
$fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["uploaded_file"]["type"]; // The type of file it is
$fileSize = $_FILES["uploaded_file"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["uploaded_file"]["error"]; // 0 for false... and 1 for true
$kaboom = explode(".", $fileName); // Split file name into an array using the dot
$fileExt = end($kaboom); // Now target the last array element to get the file extension
$_SESSION['title'] = $title;
$_SESSION['author'] = $author;
// START PHP Image Upload Error Handling --------------------------------------------------
if($fileSize > 1048576) { // if file size is larger than 1 Megabyte
echo "ERROR: Your file was larger than 1 Megabytee in size.";
unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
exit();
}
// END PHP Image Upload Error Handling ----------------------------------------------------
// Place it into your "uploads" folder mow using the move_uploaded_file() function
$moveResult = move_uploaded_file($fileTmpLoc, "uploads/$fileName");
// Check to make sure the move result is true before continuing
if ($moveResult != true) {
echo "ERROR: File has more than 1 megabyte";
exit();
}
else {$_SESSION['imageIterator'] ;} // HERE THE ITERATOR!
// ---------- Include Universal Image Resizing Function --------
include_once("ak_php_img_lib_1.0.php");
$target_file = "uploads/$fileName";
$resized_file = "resized_uploads/resized_$fileName";
$wmax = 200;
$hmax = 125;
ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
//include TextToImage class
require_once 'TextToImage.php';
//create img object
$img = new TextToImage;
//create image from text
$text = $watermark;
$img->createImage($text);
$img->saveAsPng("watermark_$fileName",'uploads_watermarks/');
// ----------- End Universal Image Resizing Function -----------
// ---------- Start Convert to JPG Function --------
if (strtolower($fileExt) != "jpg") {
$target_file = "uploads/resized_$fileName";
$new_jpg = "uploads/resized_".$kaboom[0].".jpg";
ak_img_convert_to_jpg($target_file, $new_jpg, $fileExt);
}
// ----------- End Convert to JPG Function -----------
// ---------- Start Image Watermark Function --------
$target_file = "uploads/".$kaboom[0].".jpg";
$wtrmrk_file = "uploads_watermarks/watermark_".$fileName.".png";
$new_file = "uploads_protected/protected_".$kaboom[0].".jpg";
ak_img_watermark($target_file, $wtrmrk_file, $new_file);
// ----------- End Image Watermark Function -----------
// Display things to the page so you can see what is happening for testing purposes
echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />";
?>
uj5u.com熱心網友回復:
只需在分配會話值之前檢查會話值是否已存在...
session_start();
if (!isset($_SESSION['imageIterator']) $_SESSION['imageIterator'] = 0;
uj5u.com熱心網友回復:
我認為這應該有效:
if ($moveResult != true) {
echo "ERROR: File has more than 1 megabyte";
exit();
}
else {
if(!isset($_SESSION['imageIterator'])){
$_SESSION['imageIterator'] = 1;
}else{
$_SESSION['imageIterator'] ;
}
} // HERE THE ITERATOR!
**注意:洗掉這一行:** $_SESSION['imageIterator'] = 0; // THIS SCRIPT IS EXECUTED EVERY TIME WHEN YOU UPLOAD FILES, SO EVERY TIME IT WILL BE ASSIGNED TO 0. I WANT IT ONLY ONCE
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/403473.html
標籤:
