Web Analytics Made Easy -
StatCounter Problems with uploader - CodingForum

Announcement

Collapse
No announcement yet.

Problems with uploader

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

  • Problems with uploader

    I'm having problems with my "uploader". The problem is that it's not uploading. I get this error:

    Warning: Unable to open 'C:\\WINDOWS\\Desktop\\dude.JPG' for reading: No such file or directory in /home/chatterspics/public_html/register/index.php on line 158


    And my code is:

    PHP Code:
    if ($photo != "")
                    {
                       
    // copy the file from the temporary upload position to where you want it
                        
    copy($photo"../users/$username/$username.gif") or die("<b>Error:</b> could not copy file.");

                        
    // tell them it worked
                        
    echo "";
                    }
                    else
                    {
                        
    // tell them to browse for a file
                        
    copy("nophoto.gif""../users/$username/$username.gif");
                        echo 
    "You must select a file to upload. If there was an error, please email [email][email protected][/email] with your photo, stating your username and password";
                    } 
    Anyone got any ideas on how to do it? I've looked at php.net but I got confused and more lost than I am now.

  • #2
    PHP Code:
    <?
    if($HTTP_POST_FILES['infile']['tmp_name'])
    {
     if(!
    copy($HTTP_POST_FILES['infile']['tmp_name'],$new_home_for_image.$HTTP_POST_FILES['infile']['name']))
      {
       echo 
    "doh";
      }else{
       echo 
    "whoot!";
      }
    }
    ?>

    <form action="<?=$_SERVER[PHP_SELF];?>" name="bella" method="post" ENCTYPE="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="150000">
    <input type="file" name="infile"><br />
    <input type="submit" name="submit" value="submit" style="width:230" width="230">
    </form>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

    Comment


    • #3
      I'm not sure that doing just that will work. Try reading over this one.
      PHP Code:
      <?
      $dir
      ="uploaded_files"// Directory name to upload files to
      if ($myfile){
        
      $ext substr ($myfile, -4); // Strip all but the file extension
      }
      $filetypes = array (".bmp",".jpg",".gif"); // File entensions to accept
        
      if (is_uploaded_file($myfile)){
          
      $filename=$HTTP_POST_FILES['myfile']['name'];
          
      copy ($myfile,"$dir/$filename") or die("Unable to upload file"); // Upload file
        
      }
        if (
      is_uploaded_file($myfile)){
          echo 
      "<p style=\"color:green;\">File uploaded sucessfully</p>"// On Success
        
      }else{
          echo 
      "<p style=\"color:red;\">Choose a file</p>"// Text to display before upload
        
      }
      ?>
      <form action="upload.php" enctype="multipart/form-data" method="post">
      <input type="file" name="myfile"><br><br>
      <input type="hidden" name="MAX_FILE_SIZE" value="10240000">
      <input type="submit" value="Upload">
      </form>
      Offtone.com - In the works...

      Comment


      • #4
        I'm wanting to rename the file once it's uploaded and moved to a directory. Which do I change on there to do that?

        I want it naming by their username ($username)

        Thanks for your help so far
        Last edited by Nightfire; Jul 1, 2002, 09:36 PM.

        Comment


        • #5
          well I can assure you that the one I posted earlier works ..but is_uploaded_file() or move_uploaded_file() are supposed to be safer

          PHP Code:
          <?php 
          move_uploaded_file
          ($HTTP_POST_FILES['userfile']['tmp_name'], "/place/to/put/uploaded/file".$HTTP_POST_FILES['userfile']['name']);
          ?>
          resistance is...

          MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

          Comment


          • #6
            Back

            I'm now using your uploader, firepages. I saw in devshed you gave an answer on howto rename the file when it was uploaded:

            $int = rename($filename,$newfilename);

            Where will I put this in your code?

            Sorry for being dumb

            Comment


            • #7
              As SYP}{ER points out you have to get the filetype as well


              PHP Code:
              <?
              $user
              ="joe";
              if(
              $HTTP_POST_FILES['infile']['tmp_name'])
              {
                
              $path=$new_home_for_image.$HTTP_POST_FILES['infile']['name'];
               if(!
              copy($HTTP_POST_FILES['infile']['tmp_name'],$path))
                {
                 echo 
              "doh";
                }else{
                
              $type=array('','.gif','.jpg','.png','.swf');
                
              $bits=getimagesize($path);
                
              $newname=$user.$type[$bits[2]];
                if(
              rename($path,str_replace($HTTP_POST_FILES['infile']['name'],$newname,$path)))
                  {
                    echo 
              "whoot!";
                  }
                }
              }
              ?>
              or just rename when copying

              PHP Code:
              <?
              $user
              ="joe";
              if(
              $HTTP_POST_FILES['infile']['tmp_name'])

                
              $path=$new_home_for_image.$HTTP_POST_FILES['infile']['name'];
                
              $type=array('','.gif','.jpg','.png','.swf');
                
              $bits=getimagesize($path);
                
              $newname=$user.$type[$bits[2]];
                
               if(!
              copy($HTTP_POST_FILES['infile']['tmp_name'],str_replace($HTTP_POST_FILES['infile']['name'],$newname,$path)))
                {
                 echo 
              "doh";
                }else{
                    echo 
              "whoot!";
                }
              }
              ?>
              resistance is...

              MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

              Comment


              • #8
                Thanks That worked

                Comment


                • #9
                  I'm getting an error when there's no image uploaded:

                  Warning: Unable to open 'none' for reading: No such file or directory in /home/chatterspics/public_html/register/index.php on line 189

                  PHP Code:
                  $new_home_for_image="../users/$username/";
                              if(
                  $HTTP_POST_FILES['photo']['tmp_name'])
                              {
                                
                  $path=$new_home_for_image.$HTTP_POST_FILES['photo']['name'];
                                if(!
                  copy($HTTP_POST_FILES['photo']['tmp_name'],$path))
                                {
                                 
                  copy("nophoto.gif","../users/$username/nophoto.gif");
                                }else{
                                
                  $type=array('','.gif','.jpg');
                                
                  $bits=getimagesize($path);
                                
                  $newname=$username.$type[$bits[2]];
                                 if(
                  rename($path,str_replace($HTTP_POST_FILES['photo']['name'],$newname,$path)))
                                  {
                                    echo 
                  "";
                                  }
                                }
                              } 
                  Line 189 is

                  PHP Code:
                  if(!copy($HTTP_POST_FILES['photo']['tmp_name'],$path)) 
                  I don't understand it, coz the nophoto.gif image is being copied if nothing has been uploaded, and it's also uploaded image the users are sending.
                  Last edited by Nightfire; Jul 2, 2002, 01:33 PM.

                  Comment

                  Working...
                  X