SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: probs with file upload code

  1. #1
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    IL
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    probs with file upload code

    I keep gettint the error 'Cannot Copy' when I'm running this upload page. Can anyone tell me why it wouldn't be able to upload? I can't figure this out.
    Code:
    <?
    ob_start();
    require("../Connect.php"); 
    require("../Func.php");
    $connectmysql = mysql_connect($host,$user,$password) or die ($cantconnectmysqlmessage); 
    $db = mysql_select_db($database,$connectmysql) or die ($cantconnectdatabasemessage);
    ?>
    <html><head><title>Uploads Page</title>
    <LINK REL="STYLESHEET" HREF="../CSS/AdminMain.css">
    <style type="text/css">
    body { background-image:url('../Images/TopLeft.jpg'); background-repeat:no-repeat; background-attachment:fixed } 
    </style>
    </head><body>
    <?
    if (((isset($_POST["submit"])) && ($_POST["submit"] == "Submit")))
    {
    //If the Submitbutton was pressed do: 
    //if ($_FILES['uploadfile']['type'] == "image/gif"){ 
       copy ($_FILES['uploadfile']['tmp_name'], "ftp://username:password@ftp.powweb.com/htdocs/Uploads/".$_FILES['uploadfile']['name'])  
       or die ("Could not copy"); 
            $Size=($_FILES['uploadfile']['size'])/1000000;
         $File=$_FILES['uploadfile']['name'];
         $FileGroup = $_POST["FileGroup"];
         $upload_query = "UPDATE $uploadtable SET File = '$File' WHERE TimeStamp='{$_GET['timenow']}'";
         echo $upload_query;
       if (!($upload_result      = mysql_query($upload_query))) 
        { 
         $mcrmessage = mysql_error(); 
         echo "$mcrmessage<br>"; 
         die(); 
        } 
        ELSE
        {
     header("location: Sermons.php");
    } 
    }
    else 
    {
    //If the Submitbutton was not pressed do:
    echo "<form name='form1' method='post' action='".$_SERVER["PHP_SELF"]."?timenow=".$_GET['timenow']."' enctype='multipart/form-data'>"; 
    ?>
    <p align=center><Br><Br>
    <TABLE WIDTH="75%" CELLPADDING="0" CELLSPACING="5" BORDER="0" BORDERCOLOR="#000000">
     <TR>
      <TD COLSPAN='3' ALIGN=CENTER><b><u>WEEKLY SERMONS UPLOAD</b></u></TD>
     </TR><TR>
      <TD COLSPAN='3' ALIGN=LEFT>Please select the file to upload and hit the submit button.  After some time you will be taken back to the sermon administration page.<BR><BR></TD>
     </TR>
      <TD><p align=center><INPUT TYPE='File' SIZE='40' NAME='uploadfile'></TD>
     </TR>
    </TABLE>
    <?
    echo "<br>"; 
    echo "<input type='submit' name='submit' value='Submit'><Br><br>";
    }
    ?> 
    </form>
    </body>
    </html>

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,538
    Mentioned
    79 Post(s)
    Tagged
    3 Thread(s)
    Check you have permission to upload to the directory. Also is it required that you use ftp?

    Also have a look at move_uploaded_file() rather than copy.
    Mike Swiffin - Community Team Leader

    Only a woman can read between the lines of a one word answer.....
    I started out with nothing... and still got most of it left!

  3. #3
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    IL
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Permissions for all directories are 755 and it is not required to use ftp but I couldn't get it to work with just a Uploads/ for some reason. The Uploads folder and this php file are in the same directory so I would think it would work.

    I'm curious if there isn't some kind of timeout thing going on here because it takes a while before it gives me the error. I commented everything out as far as uploading and had it echo "test"; to see what would happen and it still took forever to load that page. Should it take a while?

    Also, what do you think the benefit would be in using move_uploaded_file() instead of copy?

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you please try a simple test of FTP a file from your server directory to destination. I guess there is something in FTP as something else seems to be correct. May be copy via FTP is not working.

  5. #5
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    IL
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, FTP seems to be working fine.

    I have two accounts with this shared web host. I uploaded the same files to both accounts that are exactly the same. Both of the Uploads.php are in the same directory as a folder called 'File'. I can get it to work on my other account but not on this one for some strange reason. I'm curious if it's maybe because the other account has a domain name and this new one is not set up yet? That still doesn't make sense because I don't have the code using the domain name but it's the only thing I can think of.
    Code:
    <?
    require("Connect.php"); 
    require("Func.php");
    $connectmysql = mysql_connect($host,$user,$password) or die ($cantconnectmysqlmessage); 
    $db = mysql_select_db($database,$connectmysql) or die ($cantconnectdatabasemessage);
    ?>
    <html><head><title>Uploads Page</title><LINK REL="STYLESHEET" HREF="style.css"></head><body>
    <?
    function file_delete($file)
    { 
     chmod($file, 0777); 
     if(unlink($file))
     { 
      return true; 
     }else{ 
      return false; 
     } 
    } 
    // START REMOVE ACTION
    if ((isset($_GET["confirm"])) && ($_GET["confirm"] == "yes"))
    {
      $DispunlinkQuery = "SELECT * FROM $uploadtable WHERE ID='{$_GET['id']}'"; 
      $DispunlinkResult = mysql_query($DispunlinkQuery) or die(mysql_error().'<br />'); 
       while ($Row = mysql_fetch_assoc($DispunlinkResult)) 
        {$file="../apcnew/Files/" . $Row['DetailedFileName'];
        file_delete($file);}
      $removequery = "DELETE FROM `$uploadtable` WHERE ID='{$_GET['id']}'"; 
      $removeresult = mysql_query($removequery) or die(mysql_error().'<br />'); 
         }
    // END REMOVE ACTION
    //if(isset( $Submit )) 
    if (((isset($_POST["submit"])) && ($_POST["submit"] == "Submit")))
    {
    // Central Standard Time  
    $month = date("m");
    $day = date("d");
    $year = date("Y");
    $hour = date("H");
    $hour = $hour - 1;
    $min = date("i");
    $sec = date("s");
    $timenow = date("" . $year . "-" . $month . "-" . $day . " " . $hour . ":" . $min . ":" . $sec . "");
    // Central Standard Time
     if (!($_POST["FileGroup"] <> ""))
    { 
            echo "You must select a FILE GROUP";
            echo "<br><br><a href='http://mysite.com/Admin/Upload.php' target='body'>Back</a>";
    }
    else
     if (!($_POST["FileName"] <> ""))
    { 
            echo "You must enter a FILE NAME";
            echo "<br><br><a href='http://mysite.com/Admin/Upload.php' target='body'>Back</a>";
    }
    else
    {
     
    //If the Submitbutton was pressed do: 
    //if ($_FILES['uploadfile']['type'] == "image/gif"){ 
        copy ($_FILES['uploadfile']['tmp_name'], "Files/".$_FILES['uploadfile']['name'])  
        or die ("Could not copy"); 
            $Size=($_FILES['uploadfile']['size'])/1000000;
         $DetailedFileName=$_FILES['uploadfile']['name'];
         $FileGroup = $_POST["FileGroup"];
         $FileName = addslashes($_POST["FileName"]);  
      $upload_query = "INSERT INTO $uploadtable
      (TimeStamp, FileGroup, FileName, DetailedFileName) 
      VALUES 
      ('$timenow', '$FileGroup', '$FileName', '$DetailedFileName')";
       if (!($upload_result      = mysql_query($upload_query))) 
        { 
         $mcrmessage = mysql_error(); 
         echo "$mcrmessage<br>"; 
         die(); 
        } 
        ELSE
        {
    $Page=$_SERVER["PHP_SELF"];
    Transition($Page);
    } 
    }
    }
    else 
    {
    //If the Submitbutton was not pressed do:
    echo "<form name='form1' method='post' action='".$_SERVER["PHP_SELF"]."' enctype='multipart/form-data'>"; 
    echo "<br><select name='FileGroup'><Option Value=''>----------<Option Value='FORM'>FORM<Option Value='MANUAL'>MANUAL</select> FILE GROUP";
    echo "<br><input type='text' name='FileName'> FILE NAME"; 
    echo "<br><input type='file' name='uploadfile'>"; 
    echo "<br>"; 
    echo "<input type='submit' name='submit' value='Submit'><Br><br>";
    // START DISPLAY EVERYTHING QUERY
    $DispQuery = "SELECT * FROM $uploadtable ORDER BY FileName ASC"; 
    $DispResult = mysql_query($DispQuery) or die(mysql_error().'<br />'); 
    while ($Row = mysql_fetch_assoc($DispResult)) 
     {   
      $editlink = '<a href="UploadEdit.php?mode=edit&id='.$Row['ID'].'">Edit Link Name</a>';
      $removefile = '<a href="UploadDelete.php?mode=removefile&id='.$Row['ID'].'">Remove File</a>';
      ?>
      <table width="600" cellpadding="0" cellspacing="0" border="0" bordercolor="#000000">
       <tr>
        <td colspan="4" width="100&#37;"><hr></td>
       </tr><tr>
        <td width="40%"><a href="../apcnew/Files/<?echo $Row["DetailedFileName"];?>" target="_Blank"><?echo stripslashes($Row["FileName"]);?></a></td><td width="15%"><p align=center><?echo $Row["FileGroup"];?></td><td width="15%"><p align=center><?echo $removefile;?></td><td width="20%"><p align=center><?echo $editlink;?></td>
       </tr><tr>
        <td colspan="3" width="100%"><?echo $Row["DetailedFileName"];?></td>
       </tr>
      </table>
    <?
    }
    // END DISPLAY EVERYTHING QUERY 
    }
    ?> 
    </form> 
    </body>
    </html>

  6. #6
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    IL
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quite interesting, I asked my shared host to step in to see if there was anything they could do. Their response was that they
    set the sessions save path in the php.ini file
    Everything works fine now.

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
  •