Jump to content

noob needs help.. no errors, but code not functioning


phpnoob123

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>

 

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
     }
}

 

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • 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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.