Why won't my file upload script work?

Discussion in 'PHP' started by Imozeb, Apr 8, 2010.

  1. #1
    Why wont this work? I want to upload the file with a new file name (Example: the user uploads a file called myfile.txt and my php code uploads it as u_$vartitle.txt)

    PHP Code:
    
    $uploadfileloc = '/uploads/u_' . $vartitle . '.txt';
    $varpicurl = '/uploads/img/i_' . $vartitle . '.gif';
    					
    if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfileloc) && move_uploaded_file($_FILES['imgfilename']['tmp_name'], $varpicurl))
    {
      echo('success');
    }
    else
    {
      echo('fail');
    }
    
    Code (markup):
    Thanks.
     
    Imozeb, Apr 8, 2010 IP
  2. whlinco

    whlinco Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you should check is empty,if empty will fail,and do you upload the two files the same time?
    <form name="form1" method="post" action="file.php" enctype="multipart/form-data">
    <input type="file" name="file1">&nbsp;&nbsp;&nbsp;
    <input type="file" name="file2">&nbsp;&nbsp;&nbsp;<input type="submit" value="upload" >
    </form>
    print_r($_FILES);

    you should add these two judge condition empty($_FILES["file1"]) empty($_FILES["file2"]))
     
    whlinco, Apr 8, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I checked using

    
    if(empty($_FILES['filename']))
    {
      echo('error')
    }
    else
    {
       //code here
    }
    
    
    Code (markup):
    and they are not empty cause the code continued to execute. And yes I am using the correct form and encryption type. It can read the file when I put it through $_FILES['filename']['size'] validation, it just won't let me upload the files. Why? Is there a way I can get error information to tell me where I went wrong?
     
    Imozeb, Apr 8, 2010 IP
  4. thecancerus

    thecancerus Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    is your upload folder wirteable ... try making it's permission to 777
     
    thecancerus, Apr 8, 2010 IP
  5. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes. The directory is writable. What else could be the problem and is there anyway PHP could tell me where I went wrong like it normally does?

    Thanks.
     
    Imozeb, Apr 9, 2010 IP
  6. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #6
    
    $uploadfileloc = '/uploads/u_' . $vartitle . '.txt';
    $varpicurl = '/uploads/img/i_' . $vartitle . '.gif';
    if( (is_writable('/uploads')) && is_writable('/uploads/img')){
    $savefile = move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfileloc);
    $saveimage = move_uploaded_file($_FILES['imgfilename']['tmp_name'], $varpicurl);					
    if ($savefile && $saveimage)
    {
      echo('success');
    }
    else
    {
      echo('fail');
    }
    
    }else{
      echo 'Not Write able';
    }
    
    PHP:
    You need to check tmp_name too.
     
    guardian999, Apr 9, 2010 IP
  7. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I searched the internet somemore and found out that these PHP commands treat the document as the basename. With some extra code I was able to resolve the problem.

    Thanks everyone!
     
    Imozeb, Apr 9, 2010 IP
  8. zerophean

    zerophean Peon

    Messages:
    91
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I have same problem few months ago, You should write your form tag like this :

    <form action="action.php" method="post" enctype="application/x-www-form-urlencoded" name="form0" id="form0">
    HTML:
    enctype="application/x-www-form-urlencoded"
     
    zerophean, Apr 12, 2010 IP
  9. markup

    markup Peon

    Messages:
    116
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Following code worked for me , please create uploads/profile folders and give write permision

    HTML Form
    <form method="POST" action="mypage.php" id="thisform" enctype="multipart/form-data" >
    <input name="uploadedfile" type="file" />
    
    </form>
    
    HTML:


    mypage.php
    
    
    $target_path = "uploads/profile/".$uid."/";
    
    if(file_exists($target_path)==false ){
      mkdir($target_path, 0775);
    }
    
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    
    //echo $target_path;
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
       // echo "The file ".  basename( $_FILES['uploadedfile']['name']).
        " has been uploaded";
    }else{
       // echo "There was an error uploading the file, please try again!";
    }
    PHP:
     
    markup, Apr 12, 2010 IP