1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

file already exists & max size error

Discussion in 'PHP' started by saunders1989, Jan 16, 2010.

  1. saunders1989

    saunders1989 New Member

    Joined:
    Jan 16, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    i am trying to validate my upload form and struggling with a couple of validation methods. i want to not allow the user to upload the file larger than 5mb i have changed my php.ini file to 5M and done some coding to make it work but when i upload a file larger than 5mb i want the screen to echo something like "file too large to upload" but i dont know how to put that in. could someone advise please (code is below)

    my other question is how to check before my file is uploaded weather a file in the database has the same name. i am uploading images to a folder and storing the images path in the database please help it would be soo helpful!

    PHP:
    <?php
     
     $max_size
    =5*1024*1024;
     
    $filename =.  basename($_FILES['uploaded_file']['name']);
     
     
    // Check if a file has been uploaded
    if(isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['size']<= $max_size)
    {
         
    // Make sure the file was sent without errors
         
    if($_FILES['uploaded_file']['error'] == 0) {
             
    $target_path "images/";
            
    $target_path $target_path basename$_FILES['uploaded_file']['name']); 

        if(
    move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) {
        echo 
    "The file ".  basename$_FILES['uploaded_file']['name']). 
        
    " has been uploaded";
        
        
    $dbLink = new mysqli('localhost''root''''gallery');
             if(
    mysqli_connect_errno()) {
                 die(
    "MySQL connection failed: "mysqli_connect_error());
                                         }             
             
             
    // Gather all required data
             
    $name $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
             
    $mime $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
             
    $size intval($_FILES['uploaded_file']['size']);
             
    $image_path $dbLink->real_escape_string($target_path);
             
    $gallery_type $dbLink->real_escape_string($_POST['gallery_type']); 

        
    //query to insert the data i had gathered into the database
        
    $query "INSERT INTO `images` (`name`, `size`, `created`, `image_path`, `gallery_type_id`)
                 VALUES ('
    {$name}', {$size}, NOW(), '{$image_path}', '{$gallery_type}')";
                 
                 
    //executes the query
                     
    $dbLink->query($query);
            } 
        }
          else {
                 echo 
    'Error! A file was not sent!';
                }
    }
      
     
    // Echo a link back to the main page
     
    echo '<p>Click <a href="member-index.php">here</a> to go back</p>';
     
    ?>
     
  2. Deadly Ghos7

    Deadly Ghos7 New Member

    Joined:
    Dec 19, 2009
    Messages:
    55
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Earth
    Home Page:
    PHP:
    if ($_FILE['uploaded_file']['size'] > $max_size)
        echo 
    "<b>File too large to upload</b>";
    Put that as the one of the conditions over there. How come its hard for you when you can code so much.
    Also, for checking if the imagename is already in the database, use the SELECT query with the name of file you are uploading and if the mysql_num_rows returns value more than 0, you know the imagename is already present in the database.
     
  3. rekha_sri

    rekha_sri New Member

    Joined:
    Feb 20, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    If you want to take the uploaded filename use $_FILES['uploaded_file']['name'] .Using this you can compare the database
    filename.

    Use the following condition for checking the file size error.

    $_FILES['my_file']['error'] ==UPLOAD_ERR_INI_SIZE)||($_FILES['my_file']['error'] == UPLOAD_ERR_FORM_SIZE
     
  4. saunders1989

    saunders1989 New Member

    Joined:
    Jan 16, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    How would I check the file name is in the database? I'm already checking if it is in a folder but that's not always acurate. Could you show me please.
     

Share This Page