Hi All
I have a directory of 80+ photos
I know how to pick one file for upload just fine... but...
How can I use php to upload the entire directory?
Regards
Russ
Hi All
I have a directory of 80+ photos
I know how to pick one file for upload just fine... but...
How can I use php to upload the entire directory?
Regards
Russ
you mean like...
<html>
<body>
<table>
<?
$path = "/full/path/to/dir/";
$dir=opendir($path);
while ($file = readdir ($dir)) {
if ($file != "." && $file != ".." && eregi("\.jpg",$file)) {
echo "<tr><td><a href=\"$file\">View $file</a></td></tr>";
}
}
closedir($dir);
?>
</table>
</body>
</html>
Originally posted by WebRuss
Hi All
I have a directory of 80+ photos
I know how to pick one file for upload just fine... but...
How can I use php to upload the entire directory?
Regards
Russ
Upload an entire directory on the client machine to the server?
Obviously, PHP alone can't do the job - it runs on the server, and you need something running on the client.
That would have to go beyond plain HTML, because that only has a form field for a single file (which cannot be given an initial value for the same reason as the next point).
Javascript running on the client won't do the job either - it would be a big security hole if it were possible for site authors to write Javascript that would achieve this, as it would require giving Javascript (and hence the browser) the ability to upload arbitrary files of the site author's choosing.
But then, ftp has been around decades longer than http, and was designed for this sort of purpose - hence the name "file transfer protocol".
I appreciate your answers.
Perhaps there is a better way. I saw an article on uploading a "ZIP" file and then php perfomrs an unzip for you.
I might try that approach instead.
Anybody got an example of how that is done?
Regards
Russ
Well, the person uploading their directory would use some program to zip the files they want to upload. If they're on a Windows machine they'd proably use Winzip or something else to produce a .zip file, on *nix, it would more likely be gzip.
When PHP receives the archive file it first has to check which one was used (and indeed, check that it really is an archive file!). Then, depending, it would use either the zip ompression functions or the zlib compression functions to read the archive's contents. See the manual for the functions available, and note there also what additional libraries and configuration settings are needed for whatever your system is.
Thanks for your reply!
Well,
My idea just got shot down. My ISP does not have ZZipLib Support.
I'm going to take a look at writing an FTP function with an HTML interface. That seems at least a little better. Perhaps I can use mput to put the pictures.
I considered just loading an ftp program on the users desktop but the person who will be responsible for adding pictures is not all that computer savvy. I'm trying to make this a simple a process as possible for the user.
My final goal is an interface where the administrative user select a local directory of jpeg photos, specify a location to put them on the server,(ie. /photos/concert) and then the code takes over, uploads and creates thumbnails.
Regards
Russ
I made this class for my gallery system...
<?
set_time_limit(0);
class gallery
{
var $dir_Zip;
var $dir_Album;
var $dir_Thumb;
var $dir_ImageMagick;
function gallery($adgerd)
{
global $skra;
$this->dir_Zip = 'zip';
$this->dir_Album = 'albums';
$this->dir_Thumb = 'thumbs';
$this->dir_ImageMagick = '/usr/local/bin/convert';
if($adgerd == 'upload')
$this->upload();
elseif($adgerd == 'unzip')
$this->unzip($skra);
elseif($adgerd == 'resize')
$this->resize($skra);
}
function upload()
{
global $skra;
global $skra_name;
global $x;
?>
<form name="form1" method="post" action="" enctype="multipart/form-data">
Skrá:<br>
<input type="file" name="skra">
<br>
<br>
<input type="submit" name="x" value="Staðfesta">
</form>
<?
if($x)
{
if($skra)
{
$skra_ext = explode('.',$skra_name);
$skra_ext2 = array_reverse($skra_ext);
if($skra_ext2[0] != 'zip')
die('Skráin má einungis vera í ZIP formi.');
if(copy($skra,"zip/$skra_name"))
echo 'ZIP skrá hefur verið send.';
else
echo 'Ekki tókst að senda ZIP skrá.';
}
}
}
function unzip($skra = '')
{
if(!$skra)
{
if($dir = opendir($this->dir_Zip))
{
while($skra = readdir($dir))
{
if($skra != '.' && $skra != '..')
{
$skra_ext = explode('.',$skra);
$skra_ext2 = array_reverse($skra_ext);
if($skra_ext2[0] == 'zip')
echo "<a href='$_SERVER[PHP_SELF]?adgerd=unzip&skra=$skra'>$skra</a><br>\n";
}
}
}
}
else
{
$skra_ext = explode('.',$skra);
$skra_ext2 = array_reverse($skra_ext);
if($skra_ext2[0] != 'zip')
die('Skráin má einungis vera í ZIP formi.');
exec("unzip -d ../$this->dir_Album/$skra_ext[0] $this->dir_Zip/$skra");
exec("chmod -R 777 ../$this->dir_Album/$skra_ext[0]");
unlink("zip/$skra");
echo "Allar skrár hafa verið unZIPaðar.<br>";
if($dir = opendir("../$this->dir_Album/$skra_ext[0]"))
{
while($skra = readdir($dir))
{
if($skra != '.' && $skra != '..' && (is_file("../$this->dir_Album/$skra_ext[0]/$skra")))
{
$nSkra = str_replace('%20','_',$skra);
$nSkra = str_replace(' ','_',$nSkra);
rename("../$this->dir_Album/$skra_ext[0]/$skra","../$this->dir_Album/$skra_ext[0]/$nSkra");
}
}
echo 'Allar skrár hafa verið endurskírðar.';
}
else
{
die("Gat ekki opnað möppuna <b>../$this->dir_Album/$skra_ext[0]</b>");
}
}
}
function resize($skra = '')
{
global $quality;
global $width;
global $height;
global $iSkra;
global $x;
if(!$skra)
{
if($dir = opendir("../$this->dir_Album"))
{
?>
<form action="" method="post">
Gæði:<br>
<select name="quality">
<?
$i = 1;
while($i <= 100)
{
if($i == 90)
echo "<option value='$i' selected>$i</option>";
else
echo "<option value='$i'>$i</option>";
$i++;
}
?>
</select><br>
Width:<br>
<input type="text" name="width" size="4"> px<br>
Height:<br>
<input type="text" name="height" size="4"> px<br>
Mappa:<br>
<select name="skra">
<?
while($skra = readdir($dir))
{
if($skra != '.' && $skra != '..' && (is_dir("../$this->dir_Album/$skra")))
{
echo "<option value='$skra'>$skra</option>";
}
}
?>
</select><br><br>
<input type="submit" name="x" value="Staðfesta">
</form>
<?
}
}
else
{
if($dir = opendir("../$this->dir_Album/$skra"))
{
exec("mkdir ../$this->dir_Album/$skra/$this->dir_Thumb");
exec("chmod -R 777 ../$this->dir_Album/$skra/$this->dir_Thumb");
while($iSkra = readdir($dir))
{
if($iSkra != '.' && $iSkra != '..')
{
/*
$x = $height;
$y = $width;
if($x > $y)
{
$y = ($y / $x) * 100;
$x = 100;
}
elseif($x < $y)
{
$x = ($x / $y) * 100;
$y = 100;
}
else
{
$x = $y = 100;
}
$height = $x;
$width = $y;
*/
/*
if($image[0] > $width)
{
$imageprop = ($width * 100) / $image[0];
$imagevsize= ($image[1] * $imageprop) / 100;
$height = ceil($imagevsize);
}
*/
#print("/usr/local/bin/convert -quality $quality -geometry $width."x".$height $this->dir_Zip/$skra/$iSkra $this->dir_Zip/$skra/$this->dir_Thumb/$iSkra");
exec($this->dir_ImageMagick . ' -quality ' . $quality . ' -geometry ' . $width . 'x' . $height . ' ../' . $this->dir_Album . '/' .$skra . '/' . $iSkra . ' ../' . $this->dir_Album . '/' . $skra . '/' . $this->dir_Thumb . '/' . $iSkra);
}
}
echo 'Gerðar hafa nú verið thumbnailar úr þessum myndum.<br>';
echo "Galleryið ætti nú að vera tilbúið <a href='../index.php?album=$skra'><b>hér</b></a>";
}
}
}
}
?>