PHP to upload entire directory

Discussion in 'PHP' started by prudentialwebdev, Mar 19, 2007.

  1. #1
    I have used PHP to upload a single file, but I need it to do an entire directory, does anyone have a solution for this?
     
    prudentialwebdev, Mar 19, 2007 IP
  2. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #2
    All you need to do is get a list of the files in the directory and then use a loop to upload them. When getting the list of files in the directory, you will want to be careful to ignore directories. I would capture the file names into a array and use a loop to iterate over the individual files.
     
    clancey, Mar 19, 2007 IP
  3. prudentialwebdev

    prudentialwebdev Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thats a great strategy clancey, but I dont know how to tell php to find all the files in a user selected directory? Is this really so much simpler than its seeming to me and Im just not understanding?
     
    prudentialwebdev, Mar 19, 2007 IP
  4. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can't. PHP cannot access a user's computer.

    Your browser displays a file form field that allows you to select a local file. On submitting that form, the browser will send the selected file to the webserver. It's only after this that your PHP script is able to step in and move the temporary uploaded file.
     
    rodney88, Mar 19, 2007 IP
  5. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #5
    Maybe you want to use Java for it instead.

    Peace,
     
    Barti1987, Mar 19, 2007 IP
  6. M2D

    M2D Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You could modify this to work...
    <?php
    
    // quick "die" function
    function funk_die($msg) {
    	if($msg=='normal') {
    return "<html><body><a href=\"ftp.php\">back</a></body></html>";
    	}
    }
    
    // function to give dir listing stuff
    function dir_funk () {
    
    // cookie?
    global $ftp;
    
    // dir name
    $dir=ftp_pwd($ftp);
    // dir listing array
    $dirlist=ftp_rawlist($ftp,$dir);
    
    // print the directory listing
    $funk = "<html><body><p>current directory: <b> $dir </b></p>";
    $funk .= "<p><a href=\"ftp.php?&updir=$dir\">go up one level</a>
    </p>";
    
    $funk .= "<p>
    <table cellspacing=0 cellpadding=2 border=1>
    <tr><td><b>permissions</b></td><td><b>file/directory</b></td>
    <td><b>download</b></td><td><b>delete</b></td>
    <td><b>chmod</b></td><td><b>rename</b></td>
    </tr>
    ";
    
    	while(list($i,$thingy)=each($dirlist)) {
    
    $thingy=preg_replace("/\s+/", " ", $thingy);
    
    if($i==0) {
    echo "";
    } else {
    list($chmod,$thing,$owner,$group,$size,$month,$day,$time,$filename)=explode(" ",$thingy);
    	if(ereg("d",$chmod)) {
    $funk .= "<tr><td>$chmod</td><td><a href=\"ftp.php?dir=$dir/$filename\">$filename</a></td>
    <td>dir</td>
    <td><a href=\"ftp.php?delete=dir&thingy=$dir/$filename\">delete</a></td>
    <td><a href=\"ftp.php?chmod=1&thingy=$dir/$filename\">chmod</a></td>
    <td><a href=\"ftp.php?rename=$dir/$filename\">rename</a></td>
    </tr>";
    	} else {
    $funk .= "<tr><td>$chmod</td><td>$filename</td>
    <td><a href=\"ftp.php?dl=1&file=$dir/$filename\">d/l ($size)</a></td>
    <td><a href=\"ftp.php?delete=file&thingy=$dir/$filename\">delete</a></td>
    <td><a href=\"ftp.php?chmod=1&thingy=$dir/$filename\">chmod</a></td>
    <td><a href=\"ftp.php?rename=$dir/$filename\">rename</a></td>
    </tr>";
    	}
    }
    
    	}
    
    $funk .= "</table>
    </p>";
    
    $funk .= "<p>Create a new directory<br>
    <form action=\"ftp.php\" method=\"POST\">
    <input size=\"15\" name=\"name\">
    <input type=\"hidden\" name=\"current_dir\" value=\"$dir\">
    <input type=\"submit\" name=\"newdir\" value=\"create_dir\">
    </form>
    </p>";
    
    $funk .= "<p>Upload a local file<br>
    <form action=\"ftp.php\" method=\"POST\">
    <input type=\"file\" name=\"localfile\"><br>
    <input type=\"hidden\" name=\"current_dir\" value=\"$dir\">
    <input type=\"submit\" name=\"upload\" value=\"upload_file\">
    </form>
    </p>";
    
    $funk .= "</body></html>";
    
    return $funk;
    }
    
    
    if(!$login) {
    // print the login form
    
    echo "<html><body><p><b>ftp login</b><hr></p>
    <form action=\"ftp.php\" method=\"POST\">
    <p>Your server (eg ftp.yoursite.com)<br> <input size=\"30\" name=\"server\"></p>
    <p>Username <br> <input size=\"30\" name=\"username\"></p>
    <p>Password <br> <input size=\"30\" name=\"password\"></p>
    <p><input type=\"submit\" name=\"login\" value=\"login\"></p>
    </form></body></html>";
    } else {
    
    // try to connect or die with an error
    $ftp=ftp_connect($server) or die("invalid server");
    // try to login or die with error
    $conn=ftp_login($ftp,$username,$password) or die("login failed for some reason");
    // set a cookie so the !$login form above is never displayed
    setcookie("login","active");
    // set goody cookies
    setcookie("server",$server);
    setcookie("username",$username);
    setcookie("password",$password);
    
    	if($dir) {
    
    ftp_chdir($ftp,$dir);
    echo dir_funk();
    
    	} elseif($dl) {
    
    		if($get) {
    
    if(!$local) {
    die("please enter a value");
    }
    
    ftp_get($ftp, $local, $file, FTP_BINARY);
    echo funk_die(normal);
    
    		} else {
    echo "<html><body>";
    echo "download file";
    $size=ftp_size($ftp,$file);
    $time=ftp_mdtm($ftp,$file);
    $time=date("d M Y, H i",$time);
    echo "<p><table cellspacing=0 cellpadding=2 border=1>
    <tr><td><b>filename</b></td><td><b>size</b></td>
    <td><b>date modified</b></td></tr>
    <tr><td>$file</td><td>$size</td><td>$time</td></tr></table>";
    echo "<form action=\"ftp.php\" method=\"POST\">
    where to download the file to (/home/me/file.txt or c:/file.txt):<br>
    <INPUT NAME=\"local\" size=\"40\">
    <input type=\"hidden\" name=\"dl\" value=\"1\">
    <input type=\"hidden\" name=\"file\" value=\"$file\"><br>
    <INPUT TYPE=\"submit\" name=\"get\" VALUE=\"download file\">
    </FORM>";
    echo "</body></html>";
    
    		}
    
    	} elseif($rename) {
    
    		if($do) {
    
    if(!$val) {
    die("please enter a value");
    }
    
    ftp_rename($ftp, $rename, $val);
    echo funk_die(normal);
    
    		} else {
    
    echo "<html><body>
    rename this file: $rename
    <p>
    <form action=\"ftp.php\" method=\"POST\">
    Rename to: <input size=\"50\" name=\"val\" value=\"$rename\"><br>
    <input type=\"hidden\" name=\"rename\" value=\"$rename\">
    <input type=\"submit\" name=\"do\" value=\"rename\">
    </form>
    </body></html>";
    
    		}
    
    	} elseif($chmod) {
    
    		if($do) {
    
    if(!$val) {
    die("please enter a value");
    }
    
    $cmd="CHMOD $val $file";
    ftp_site($ftp, $cmd);
    echo funk_die(normal);
    
    		} else {
    
    echo "<html><body>
    chmod this file: $thingy
    <p>
    <form action=\"ftp.php\" method=\"POST\">
    <input size=\"4\" name=\"val\" maxlength=\"4\"> CHMOD value<br>
    <input type=\"hidden\" name=\"file\" value=\"$thingy\">
    <input type=\"hidden\" name=\"chmod\" value=\"1\">
    <input type=\"submit\" name=\"do\" value=\"chmod\">
    </form>
    </body></html>";
    
    		}
    
    	} elseif($delete) {
    
    		if($delete=='file') {
    ftp_delete($ftp, $thingy);
    		} elseif($delete=='dir') {
    ftp_rmdir($ftp, $thingy);
    		}
    echo funk_die(normal);
    
    	} elseif($upload) {
    
    if(!$localfile) { die("please enter a file"); }
    
    $file = substr( strrchr( $localfile, "/" ), 1 );
    $put_thingy = $current_dir . "/" . $file;
    ftp_put($ftp, $put_thingy, $localfile, FTP_BINARY);
    echo funk_die(normal);
    
    	} elseif($newdir) {
    	
    if(!$name) { die("please enter a name"); }
    
    $newdir = $current_dir . "/" . $name;
    ftp_mkdir($ftp,$newdir);
    echo funk_die(normal);
    
    	} elseif($updir) {
    
    $newdir = str_replace( substr( strrchr( $updir, "/" ), 1 ) , "", $updir);
    
    ftp_chdir($ftp,$newdir);
    echo dir_funk();
    
    	} else {
    
    echo dir_funk();
    
    	}
    }
    
    ?>
    Code (markup):
     
    M2D, Mar 19, 2007 IP