SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru SharifTK's Avatar
    Join Date
    Jan 2004
    Location
    New York, NY
    Posts
    621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem uploading an image

    I am trying to upload a file to a certain directory, while checking to see if the file is either a .jpg, .jpeg, or a .gif file and it has to be 80x80.

    here is the form code:
    HTML Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>?act=news&code=03" method="post" enctype="multipart/form-data" name="upload_icon_form">
      <div id="form"> 
        <p>Icon Location:<br>
          <input name="icon" type="file" class="input" id="icon">
        </p>  
        </div>
    <p></p>
      <div><b>Please do not press the "Upload Icon" button more than once!</b></div>
    <div id="form" style="text-align: center;">
    <p>
          <input name="submit" type="submit" value="Upload Icon" class="input">
          &nbsp;<input name="reset" type="reset" value="Reset" class="input"></p>
    </div>
    </form>
    and the php:
    PHP Code:
    function upload_icon()
    {
        global 
    $STAFF;
        global 
    $ERROR;
        
        
    $upload_dir $SITE['images'] . 'news_icons/';
        
    $max_width 80;
        
    $max_height 80;
        
        if(isset(
    $_POST['submit']) && $_POST['submit'] == 'Upload Icon')
        {
            
    //check if a file was specified
            
    if(!isset($_POST['icon']) || $_POST['icon'] == NULL)
            {
                
    //Get da form!
                
    echo '<p><font color="#FF0000">';
                echo 
    'You dummy, you didn\'t select a file to upload!';
                echo 
    '</font></p>';
                include(
    $STAFF['forms'] . 'upload_icon.php');
            }
            else
            {
                
    $file $_FILES['icon']['tmp_name'];
                
    $dimensions getimagesize($file);
                
    $width $dimensions[0];
                
    $height $dimensions[1];
            
                
    //Check image type
                
    if($_FILES['icon']['type'] != 'image/pjpeg' || $_FILES['icon']['type'] != 'image/gif')
                {    
                    
    unlink($file);
                    unset(
    $_FILES);
                    
                    
    //Get da form!
                    
    echo '<p><font color="#FF0000">';
                    echo 
    'The file selected was not a in .jpg, .jpeg, or .gif format!';
                    echo 
    '</font></p>';
                    include(
    $STAFF['forms'] . 'upload_icon.php');
                }
                
    //Check image dimensions
                
    elseif($width != $max_width || $height != $max_height)
                {
                    
    unlink($file);
                    unset(
    $_FILES);
                    
                    
    //Get da form!
                    
    echo '<p><font color="#FF0000">';
                    echo 
    'The file selected is not 80x80 pixels!';
                    echo 
    '</font></p>';
                    include(
    $STAFF['forms'] . 'upload_icon.php');
                }
                else
                {
                    echo 
    '...copying...';
                }
            }
        }
        else
        {
            
    //Get da form!
            
    echo '<p style="text-align: left;">';
            echo 
    'You can use this form to upload new news icons. Remember that the image MUST be in either .jpg, .jpeg or .gif format. ';
            echo 
    'Also note that the image must be 80x80 pixels.';
            echo 
    '</p>';
            include(
    $STAFF['forms'] . 'upload_icon.php');
        }

    The problem is that the file upload field doesn't seem to pass the data to the php because even if a file is selected to upload, i get the error I get that is supposed to diplay when a file isn't selected. Does anyone see ANYTHING wrong with this code?
    SK

  2. #2
    Romans 12:2 codyrockx's Avatar
    Join Date
    Jul 2003
    Location
    Newberg, Oregon
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    pass the post and files arrays to the function like so,
    PHP Code:
    function upload_icon($_POST,$_FILES)
       
    //function to upload 


    upload_icon($_POST,$_FILES); 
    Codyrobert.com - Designer and Developer

  3. #3
    SitePoint Guru SharifTK's Avatar
    Join Date
    Jan 2004
    Location
    New York, NY
    Posts
    621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The form points to a file that executes that function. What is the point of passing superglobals?
    SK

  4. #4
    Romans 12:2 codyrockx's Avatar
    Join Date
    Jul 2003
    Location
    Newberg, Oregon
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm, well i misunderstood that, the problem with it is its not sending the $_POST[icon] variable for some reason. Try checking whether or not they specifed the file with
    PHP Code:
    if(!isset($_FILES[icon][tmp_name] || $_FILES[icon][tmp_name] == NULL){ 
    Codyrobert.com - Designer and Developer

  5. #5
    SitePoint Guru SharifTK's Avatar
    Join Date
    Jan 2004
    Location
    New York, NY
    Posts
    621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well changing that works with when checking if a file was specified. But then the second part, to check is the file is a jpeg or a gif doesn't work. Even if it IS one of those types, i still get my message.
    SK

  6. #6
    SitePoint Guru SharifTK's Avatar
    Join Date
    Jan 2004
    Location
    New York, NY
    Posts
    621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok fixed all that, now this is the problem:
    PHP Code:
    $file $_FILES['icon']['tmp_name'];
    $upload_dir 'icons/';
    move_uploaded_file($file$upload_dir $_FILES['icon']['name']); 
    It returns this error:

    Warning: move_uploaded_file(icons/icon2.jpg): failed to open stream: No such file or directory in /file.php on line 333

    Warning: move_uploaded_file(): Unable to move '/tmp/phpawrAU5' to 'icons/icon2.jpg' in /file.php on line 333
    SK


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •