Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old May 8th, 2007, 12:19 PM
Registered User
Join Date: May 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with images upload

I Just saw a images upload script and i am wandering how to print an error if i do not select a filem.

the script:

//define a maxim size for the uploaded images in Kb
 define ("MAX_SIZE","100"); 

//This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;

//This variable is used as a flag. The value is initialized with 0 (meaning no error  found)  
//and it will be changed to 1 if an errro occures.  
//If the error occures the file will not be uploaded.
//checks if the form has been submitted
     //reads the name of the file the user submitted for uploading
     //if it is not empty
     if ($image) 
     //get the original name of the file from the clients machine
         $filename = stripslashes($_FILES['image']['name']);
     //get the extension of the file in a lower case format
          $extension = getExtension($filename);
         $extension = strtolower($extension);
     //if it is not a known extension, we will suppose it is an error and will not  upload the file,  
    //otherwise we will do more tests
 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
        //print error message
             echo '<h1>Unknown extension!</h1>';
//get the size of the image in bytes
 //$_FILES['image']['tmp_name'] is the temporary filename of the file
 //in which the uploaded file was stored on the server

//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
    echo '<h1>You have exceeded the size limit!</h1>';

//we will give an unique name, for example the time in unix time format
//the new name will be containing the full path where will be stored (images folder)
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
    echo '<h1>Copy unsuccessfull!</h1>';

//If no errors registred, print the success message
 if(isset($_POST['Submit']) && !$errors) 
     echo "<h1>File Uploaded Successfully! Try again!</h1>";


 <form name="newad" method="post" enctype="multipart/form-data"  action="">
     <tr><td><input type="file" name="image"></td></tr>
     <tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>


Old May 11th, 2007, 07:25 PM
Registered User
Join Date: Jan 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to cyber_kaley

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml">
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <title>Upload a File</title>
<?php // Script 11.4 - upload_file.php
// This script displays and handles an HTML form.
// This script takes a file upload and stores it on the server.

// Address error handing.
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);

if (isset ($_POST['submit'])) { // Handle form.

    // Try to move the uploaded file.
    if (move_uploaded_file ($_FILES['thefile']['tmp_name'], "../uploads/{$_FILES['thefile']['name']}")) {

        print '<p>Your file has been uploaded.</p>';

    } else { // Problem!

        print '<p>Your file could not be uploaded because: <b>';

        // Print a message based upon the error.
        switch ($_FILES['thefile']['error']) {
            case 1:
                print 'The file exceeds the upload_max_filesize setting in php.ini';
            case 2:
                print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form';
            case 3:
                print 'The file was only partially uploaded';
            case 4:
                print 'No file was uploaded';
        print '</b>.</p>';

} // End of SUBMIT IF.

// Leave PHP and display the form.

<form action="upload_file.php" enctype="multipart/form-data" method="post">
<p>Upload a file using this form: <br /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input type="file" name="thefile" /><br /><br />
<input type="submit" name="submit" value="Upload This File" />


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to upload images? vipin k varghese Beginning PHP 1 June 3rd, 2008 03:59 AM
Cannot upload a whole images vnguyen Java Databases 1 July 25th, 2005 03:46 PM
how I can show and upload a images Alexds ASP.NET 1.x and 2.0 Application Design 2 May 19th, 2005 04:14 PM
Upload Images Help leeyoung Classic ASP Professional 0 April 26th, 2005 08:42 PM
upload images mccoco Classic ASP Professional 2 September 23rd, 2003 04:30 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.