Web Analytics Made Easy -
StatCounter file upload - CodingForum

Announcement

Collapse
No announcement yet.

file upload

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • file upload

    whats wrong with this?


    PHP Code:


    $username  
    =  $username1."".$username."";
    $password  =  $password1."".$password."";
    $avatar =  $avatar1."".$avatar."";



    if  (empty(
    $avatar))
    {
        print  
    "No avatar was selected.";
    }
    else
    {
    $tempFile  =  $HTTP_POST_FILES['avatar']['avatar.gif'];
    $destination  =  "/var/www/html/forum/users/$username/"  .
        
    $HTTP_POST_FILES['avatar']['avatar.gif'];
    copy($tempFile,  $destination);


    what its supposed to do is upload an image (.gif) and put it in the directory "/var/www/html/forum/users/$username/" and name image avatar.gif... but it doesnt do anything...heres the code for the previous page where you chose the file..


    PHP Code:
    <form method="post" action="/forum/changeavatarprocessor.php">
    Username: <input type="text" size="20" name="username" maxlength="16">
    <
    br>
    Password: <input type="password" size="20" name="password" maxlength="16">
    <
    br><br>
    Avatar: <input type="file" value="" name="avatar" size="20">
    <
    br>
    <
    br>
    <
    input type="submit" value="Change Avatar">
    </
    form
    ~Designer's Toolz~

  • #2
    nevermind. I played around with it and used this:


    PHP Code:
    if  (empty($avatar))
    {
        print  
    "No avatar was selected.";
    }
    else
    {
    copy($HTTP_POST_FILES['avatar'] ['tmp_name'],
    $HTTP_POST_FILES['avatar'] ['name']);
    copy("$avatar",  "/var/www/html/forum/users/$username/avatar.gif");
    unlink("$avatar");

    ~Designer's Toolz~

    Comment


    • #3
      I'm surprised that works

      You want to check the file is an image, or anyone can upload an exe file or even worse, a virus

      You need this in the <form> tag ENCTYPE="multipart/form-data"

      You need to use these instead of $HTTP_POST_FILES

      $_FILES['myfile']['tmp_name'] -- Contains the full path and filename of the uploaded file as stored on the server.

      $_FILES['myfile']['name'] -- Contains the full path and filename of the uploaded file as stored on the client.

      $_FILES['myfile']['size'] -- Contains the size of the file in bytes

      $_FILES['myfile']['type'] -- Contains the MIME type information associated with the file (if any)


      Should have something like this: (taken from zend.com and updated)
      PHP Code:
      <HTML> 
      <HEAD> 
      <TITLE>File Upload Results</TITLE> 
      </HEAD> 
      <BODY BGCOLOR="WHITE" TEXT="BLACK"> 
      <P><FONT FACE="Arial, Helvetica, sans-serif"><FONT SIZE="+1">File Upload 
          Results</FONT><BR><BR> 
      <?php 
           
          $uploadpath 
      '/path/to/store/uploaded/files/'
          
      $source $_FILES['file1']['tmp_name']; 
          
      $dest ''

          if ( (
      $source != 'none') && ($source != '' )) { 

              
      $imagesize getimagesize($source); 

              switch ( 
      $imagesize[2] ) { 

                  case 
      0

                      echo 
      '<BR> Image is unknown <BR>'
                      break; 

                  case 
      1
                      echo 
      '<BR> Image is a GIF <BR>'
                      
      $dest $uploadpath.uniqid('img').'.gif'
                      break; 
                   
                  case 
      2
                      echo 
      '<BR> Image is a JPG <BR>'
                      
      $dest $uploadpath.uniqid('img').'.jpg'
                      break; 
                   
                  case 
      3
                      echo 
      '<BR> Image is a PNG <BR>'
                      
      $dest $uploadpath.uniqid('img').'.png'
                      break; 

              } 

              if ( 
      $dest != '' ) { 

                  if ( 
      move_uploaded_file$source$dest ) ) { 

                      echo 
      'File successfully stored.<BR>'

                  } else { 

                      echo 
      'File could not be stored.<BR>'

                  } 

              }  

          } else { 

              echo 
      'File not supplied, or file too big.<BR>'

          } 

      ?> 
      <BR><A HREF="<?php echo $_SERVER['PHP_SELF'?>">Back</A> 
      </FONT></P> 
      </BODY> 
      </HTML> 
      <?php } else { ?> 
      <!-- File Upload Form HTML Code Here --> 
      <?php ?>
      [

      Comment


      • #4
        Yeah thats a no brainer but thanks for the warning. I had already changed that on the form page... i specified max file size and accept type. thanks though
        ~Designer's Toolz~

        Comment


        • #5
          <input type="hidden" name="MAX_FILE_SIZE" value="10000" />


          <input type="file" name="avatar" accept="image/gif" />


          a lot simpler
          ~Designer's Toolz~

          Comment

          Working...
          X