PDA

View Full Version : [php fopen()] unzip uploaded file to created newly created directory



[ paul ferrie ]
January 27th, 2006, 07:45 AM
Hi guys.
Right i have already spent the best part of 8 hours trying to get this working.
What i am trying to do:
Upload zip file to directory. Check if upload is successfull and then unzip to directory created by name of zip file.
I can upload the zip file and create the diectory but the damn files wont unzip to the newly created directory:(


$folderName = substr($_FILES['Filedata']['name'],0,-4);
$folderPath="D:/www/htdocs/triplej/media/shoots/images/";
if(move_uploaded_file($_FILES['Filedata']['tmp_name'], $folderPath.$_FILES['Filedata']['name'])){
$zip = zip_open($folderPath.$_FILES['Filedata']['name']);
mkdir($folderPath.$folderName);
if($zip !== false) {
while ($zip_entry = zip_read($zip)) {
if (zip_entry_open($zip, $zip_entry, "r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
$fp = fopen($folderPath.$folderName."/","w");
fwrite($fp,$buf);
zip_entry_close($zip_entry);
}
echo "\n";
}
}
}else {
echo "zip file could not be found!";
}

i keep on getting this error


Warning: fopen(D:/www/htdocs/triplej/media/shoots/images/pictest/): failed to open stream: Permission denied

But yet if i change


($folderPath.$folderName."/","w");
//to
($folderPath. zip_entry_name($zip_entry)."/","w");

It upzips the images to the images folder and not the newly created folder. In testing the zip file is called pictest.zip and the folder created is pictest.
thanks for any help on this.
Paul

bigmtnskier
January 30th, 2006, 01:56 AM
Argh! I just finished typing a reply and pressed "Go Advanced" and it logged me out. Oh well, here it is:

I'm not really familiar with the zip functions, that's probably apparent :P



$i = 0;
$folderName = substr($_FILES['Filedata']['name'],0,-4);
$folderPath="D:/www/htdocs/triplej/media/shoots/images/";
if(move_uploaded_file($_FILES['Filedata']['tmp_name'], $folderPath.$_FILES['Filedata']['name'])){
$zip = zip_open($folderPath.$_FILES['Filedata']['name']);
mkdir($folderPath.$folderName);
//Attempt to chmod the directory to allow writing
//If this was a linux server, you would want 0777 instead of 666
chmod($folderPath.$folderName."/", 666);
if($zip !== false) {
while ($zip_entry = zip_read($zip)) {
//Im not familiar with the zip functions :P So I could be totally wrong.
$i++;
if (zip_entry_open($zip, $zip_entry, "r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
//Trying to open a folder for writing?
//$fp = fopen($folderPath.$folderName."/","w");
//I'm not sure this is what you want... but it's worth a try
$fp = fopen($folderPath.$folderName."/somefile".$i.".ext","w");
fwrite($fp,$buf);
zip_entry_close($zip_entry);
}
echo "\n";
}
}
}else {
echo "zip file could not be found!";
}


I hope this helps! I the main problem was the fopen attempting to open and write to the directory, not a file. I think I changed it so that it should work.

-Bigmtnskier :)

[ paul ferrie ]
January 30th, 2006, 06:17 AM
You are going a long the right lines...

I got it sorted :)


$fp = fopen($folderPath.$folderName."/".zip_entry_name($zip_entry,"w");