Jump to content

Recommended Posts

Ok  so for my code, i need to create a new folder on my cpanel server by user input (html form)...  The folder is created, then I want the user to click a button which will have two functions...  one to copy a ZIP file to that (user generated) folder...  and two...  EXTRACT that ZIP file in the folder.

 

Here is my code...  it has no errors...  the folder is being created by the user input, but the ZIP file is not copied into it, and is obviously not being extracted...

 

PS...  please don't laugh at my code..  im only coding for a few  short months.

 

<?php
//Get folder name from form string
$_SERVER['QUERY_STRING'];
$folderName = $_POST['folderName'];

//Path to where you want to create the new folder
$path = "folders/";

//Complete path to new folder
$d = $path . $folderName;

mkdir($d, 0755);

echo "Folder " . $d . " has been created.";

echo '<br />_________________________________________________________________<br /><br />';

echo "Lets get started...  click next to continue";


if(isset($_POST['submit'])) {
copy_zip_file();
extract_zip_file();
}

function copy_zip_file(){
$old = 'test.zip';
$new = '/folders/$folderName/test.zip';
copy($old, $new) or die("Unable to copy $old to $new.");
}

function extract_zip_file(){
     $zip = new ZipArchive;
     $res = $zip->open(’folders/$folderName/test.zip’);
     if ($res === TRUE) {
         $zip->extractTo(’folders/$folderName/’);
         $zip->close();
         echo ‘ok’;
     } else {
         echo ‘failed’;
     }
}
?>
<form action="nav.php" name="submit" method="post">
<input class="button" type="submit" value="Submit">
</form>

 

 

 

 

 

Ok, so replace this:

 

if(isset($_POST['submit'])) {
copy_zip_file();
extract_zip_file();
}

function copy_zip_file(){
$old = 'test.zip';
$new = '/folders/$folderName/test.zip';
copy($old, $new) or die("Unable to copy $old to $new.");
}

 

with this

 

if(isset($_POST['submit'])) {
if(exec("cp ".$old." ".$new))
{
  extract_zip_file();
} else {
   die("Unable to copy $old to $new");
}
}

 

See if that works.

I really think your problem is here (in copy_zip_file()):

// THIS
$new = '/folders/$folderName/test.zip';

//SHOULD BE THIS
$new = "folders/$folderName/test.zip";

That is not the same path where you just created the folder.  The slash "/" at the beginning of the path indicates the root of the server's filesystem, I seriously doubt that that is the path to your folders directory. Remove that first slash and use double-quotes and you should be golden.

 

Also, you are using "smart quotes" (which I don't think are really that smart ...) in the extract_zip_file() function. See the ones that are leaning to the right or left? They need to be single quotes or double-quotes like they are in the rest of your code. (The ones with variable names in them, need to be double-quotes):

function extract_zip_file(){
     $zip = new ZipArchive;
     $res = $zip->open("folders/$folderName/test.zip"); // This line
     if ($res === TRUE) {
         $zip->extractTo("folders/$folderName/"); // This line
         $zip->close();
         echo 'ok'; // This line
     } else {
         echo 'failed'; // This line
     }
}

 

Thank guys...  Just tryed both solutions... but no luck =/

 

This is frustrating.. such a simple task (i thought)

 

The problem lies with the copying of the file..  its just not duplicating the file at all..

 

It makes the folder correctly (with user input as the foldername)

But no copy of the ZIP file?

 

stumped..

Are you sure the zip file is in the directory with the script?

 

Turn on error reporting. (Add these two lines immediately after your openning PHP tag:

error_reporting(E_ALL);
ini_set('display_errors', 1);

 

Post any messages you receive along with your current code.

Error reporting on..  I see no errors..  I tryed both suggestions..  but here is the code I have now;

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

//Get folder name from form string
$_SERVER['QUERY_STRING'];
$folderName = $_POST['folderName'];
$path = "folders/";
//Complete path to new folder
$d = $path . $folderName;

mkdir($d, 0755);

echo "Folder " . $d . " has been created.";
echo '<br />_________________________________________________________________<br /><br />';
echo "Lets get started...  click next to continue";


if(isset($_POST['submit'])) {
copy_zip_file();
extract_zip_file();
}

function copy_zip_file(){
$old = "test.zip";
$new = "folders/$folderName/test.zip";
copy($old, $new) or die("Unable to copy $old to $new.");
}

function extract_zip_file(){
     $zip = new ZipArchive;
     $res = $zip->open(’folders/$folderName/test.zip’);
     if ($res === TRUE) {
         $zip->extractTo(’folders/$folderName/’);
         $zip->close();
         echo ‘ok’;
     } else {
         echo ‘failed’;
     }
}
?>
<form action="nav.php" name="submit" method="post">
<input class="button" type="submit" value="Submit">
</form>

 

STOP THE PRESSES...    just contacted my hosting co..    copy() and shell not allowed..  =(

 

Thanks guys..  good to know that help is available! very much appreciated and happy new year.

 

Who are you hosted with?

Your form does not have a field named submit, so your code is never being executed because if(isset($_POST['submit'])) { will never be TRUE.

 

Fix your form so that it actually contains a form field with name='submit'

  • 4 weeks later...

Dear sirs...

 

I´m sorry for asking but can you assist me?

This php script is exactly what i was looking for, but it dosen´t work for me rather then creating a file "folders"

 

Here´s error report:

"Notice: Undefined index: folderName in /hsphere/local/home/gdr2010/webrestaurante.info/teste/teste/teste4.php on line 7 Folder folders/ has been created."

 

How can i define the index folder?

Thanks for the reply and sorry for the trouble.

 

 

Dear sirs...

 

I´m sorry for asking but can you assist me?

This php script is exactly what i was looking for, but it dosen´t work for me rather then creating a file "folders"

 

Here´s error report:

"Notice: Undefined index: folderName in /hsphere/local/home/gdr2010/webrestaurante.info/teste/teste/teste4.php on line 7 Folder folders/ has been created."

 

How can i define the index folder?

Thanks for the reply and sorry for the trouble.

 

Please start a new thread for your issue.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.