PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY If (Web server extensions)
PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY If you cast your mind back to Chapter 6, PHP stores an upload file in a temporary location until you move it to its target location. This temporary file is accessed using the tmp_name element of the $_FILES superglobal array and is discarded when the script ends. Instead of moving the temporary file to the upload folder, you can adapt the script in create_thumb.inc.php to resize the image, and save the scaled-down version instead. 3. The form in upload.php uses image as the name attribute of the file upload field, so the original image (referred to as $original) is now in $_FILES[’image’][’tmp_name’]. Change the opening section of the code like this (new code is in bold): // define constants define(’THUMBS_DIR’, ‘C:/upload_test/thumbs/’); define(’MAX_WIDTH’, 120); define(’MAX_HEIGHT’, 90); // process the uploaded image if (is_uploaded_file($_FILES[’image’][’tmp_name’])) { $original = $_FILES[’image’][’tmp_name’]; // begin by getting the details of the original list($width, $height, $type) = getimagesize($original); This removes the definition of SOURCE_DIR, which is no longer needed, and simplifies the original if… else statements at the beginning of the script. The code in upload.php takes care of checking that a file has been selected, so all that s needed here is to use is_uploaded_file() to check that the temporary file is a genuine upload and to assign it to $original. If you ever had any doubts, this should convince you just how useful variables are. From this point on, the script treats the temporary upload file in exactly the same way as a file already on the server. The remaining steps also demonstrate the value of recycling code. 4. Save create_thumb.inc.php. The rest of the changes are made in the upload file. 5. Open upload.php from Chapter 6 and save it as upload_thumb.php. 6. Locate the following section of code in upload_thumb.php (it should be around lines 32 through 60): if ($sizeOK && $typeOK) { switch($_FILES[’image’][’error’]) { case 0: // $username would normally come from a session variable $username = ‘davidp’; // if the user’s subfolder doesn’t exist yet, create it if (!is_dir(UPLOAD_DIR.$username)) { mkdir(UPLOAD_DIR.$username); } // check if a file of the same name has been uploaded if (!file_exists(UPLOAD_DIR.$username.’/’.$file)) {
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.