2012-04-24 4 views
1
から呼び出すとしないと、

私はちょっと問題に悩まされています。PHPの解凍機能はうまく動作しますが、httpdocs/

zipファイルとそのフォルダを解凍するためにいくつかの関数を使用していますが、これはうまくいきます。しかし、私が他のディレクトリから関数を使用しようとすると、動作していないように見えるか、フォルダdirsを作成してファイルを書き込んでいないようです。

次のコードを使用して、.zipファイルを解凍しています。

<?PHP error_reporting(e_all); 

include("../../includes/config.php"); 


/** 
* Unzip the source_file in the destination dir 
* 
* @param string  The path to the ZIP-file. 
* @param string  The path where the zipfile should be unpacked, if false the directory of the zip-file is used 
* @param boolean  Indicates if the files will be unpacked in a directory with the name of the zip-file (true) or not (false) (only if the destination directory is set to false!) 
* @param boolean  Overwrite existing files (true) or not (false) 
* 
* @return boolean  Succesful or not 
*/ 

function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true) 
{ 
    if ($zip = zip_open($src_file)) 
    { 
    if ($zip) 
    { 
     $splitter = ($create_zip_name_dir === true) ? "." : "/"; 
     if ($dest_dir === false) $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/"; 

     // Create the directories to the destination dir if they don't already exist 
     create_dirs($dest_dir); 

     // For every file in the zip-packet 
     while ($zip_entry = zip_read($zip)) 
     { 
     // Now we're going to create the directories in the destination directories 

     // If the file is not in the root dir 
     $pos_last_slash = strrpos(zip_entry_name($zip_entry), "/"); 
     if ($pos_last_slash !== false) 
     { 
      // Create the directory where the zip-entry should be saved (with a "/" at the end) 
      create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1)); 
     } 

     // Open the entry 
     if (zip_entry_open($zip,$zip_entry,"r")) 
     { 

      // The name of the file to save on the disk 
      $file_name = $dest_dir.zip_entry_name($zip_entry); 

      // Check if the files should be overwritten or not 
      if ($overwrite === true || $overwrite === false && !is_file($file_name)) 
      { 
      // Get the content of the zip entry 
      $fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); 

      file_put_contents($file_name, $fstream); 
      // Set the rights 
      chmod($file_name, 0777); 
      echo "save: ".$file_name."<br />"; 
      } 

      // Close the entry 
      zip_entry_close($zip_entry); 
     }  
     } 
     // Close the zip-file 
     zip_close($zip); 
    } 
    } 
    else 
    { 
    return false; 
    } 

    return true; 
} 

/** 
* This function creates recursive directories if it doesn't already exist 
* 
* @param String The path that should be created 
* 
* @return void 
*/ 

function create_dirs($path) 
{ 
    if (!is_dir($path)) 
    { 
    $directory_path = ""; 
    $directories = explode("/",$path); 
    array_pop($directories); 

    foreach($directories as $directory) 
    { 
     $directory_path .= $directory."/"; 
     if (!is_dir($directory_path)) 
     { 
     //mkdir($directory_path); 

     my_ftp_mkdir2('httpdocs/templates/temp/', $directory_path); 

     //chmod($directory_path, 0777); 
     } 
    } 
    } 
} 

/* CHMOD with FTP */ 

function my_ftp_mkdir2 ($path, $dir) { 
    $server = "ftp.domainname.com"; 

    // Connect to FTP server 
    $connection = ftp_connect ($server); 

    // Login on FTP server 
    $user = "username"; 
    $pass = "password"; 
    $result = ftp_login ($connection, $user, $pass); 

    if ((!$connection) || (!$result)) { 
     return false; 
    } 
    // Go to the dir thats been given 
    if ([email protected]_chdir ($connection, $path)) { 
     ftp_close($connection); // close FTP connection 
     return false; 
    } 

    // Create the map and set the rights 
    if ([email protected]_mkdir($connection, $dir)) { 
     ftp_close($connection); // close FTP connection 
     return false;  
    } 

    $chmod_cmd = "CHMOD 0777 " . $dir; 
    $chmod = ftp_site($connection, $chmod_cmd); 

    ftp_close($connection); // close FTP connection 

    return true; 
} 



if (!unzip("../user1/test2.zip", false, true, true)) { 
    echo 'The file has failed to be unzipped!'; 
} else { 
    echo 'The file has been unzipped!'; 
} 


?> 

これは素晴らしいです。

しかし、次のされたテストディレクトリにこのファイルを実行しているイム: をhttpdocs /テンプレート/ TEMP/unzip.php

test2.zipと呼ばれるファイルは次の場所にあります。 をhttpdocs /テンプレート/ USER1/TEST2私は、スクリプトを実行したときに.zipファイル

だから、それが作る次 をhttpdocs /テンプレート/ USER1/TEST2/index.htmlを をhttpdocs /テンプレート/ USER1/TEST2/IMG/ などなど。

しかし、私がこのスクリプトをhttpdoから実行するとcs /ディレクトリには解凍されていますが、ディレクトリやディレクトリは作成されていません。

私のPHPは、テスト目的で777にFTPをchmodするためにFTPを使用した理由について、セーフモードを持っています。

私はをhttpdocs/unzip.php

でこのスクリプトを実行したときので、私はこのような関数を呼び出します。

<?php 
if (!unzip("templates/user1/test2.zip", false, true, true)) { 
    echo 'The file has failed to be unzipped!'; 
} else { 
    echo 'The file has been unzipped!'; 
} 
?> 

これはうまくいかないようですが、誰かが助けてくれることを願っています。ありがとう!あなたのウェブサーバの利用者にはhttpdocsの所有者を設定し

答えて

0

(apacheのそのWWW-データのため通常は)

chown www-data:www-data /path/to/htttpdocs -R 

注:私はあなたのユーザ名で最初のWWW-データを交換しますが、ファイルに自分自身をアクセスする必要があると思います例:user123:www-data

注2:Webサーバーのエラーログを確認すると、そこに正確なエラーメッセージが表示されます。 (Apache standartパスは/var/log/apache2/error.logです)

注3:SafemodeはPHP 5.4で削除されています。私はあなたに更新を提案します。

+0

ご返信ありがとうございますが、httpdocsの所有者は既にwebserversユーザーに設定されており、何の違いもありません。 – Adriaan

+0

関数呼び出しで絶対パスを使用してみてください。 – peipst9lker

+0

@Adriaanあなたはエラーログにいくつかの有用な情報を見つけましたか? – peipst9lker

0

は私はそれが絶対パス&

mkdir($abspathdir,0777) 

(アクセス許可をリセットするために後& umask)を使用して行うだろう

おそらくすっきり&速い(?)新しいフォルダを作成するには、FTP接続をオープンする理由?

デバッグするので、ディレクトリが作成された(されるようにしようとして)されているチェックするには、mkdir前

echo $abspathdir,'<br/>'; 

を追加します。実際にpathが正しくてもdirが作成されていない場合は、親フォルダのアクセス権の問題のみになります。

+0

私はmkdirを使用するとdirを作成しますが、所有者をapacheとして設定し、ftm機能を使用しなければならない理由で適切な権限でchmodを実行しません。私は、セーフモードがONであるので、mkdirが正常に動作していないと思います。なぜFTP機能を使って回避したのですか? – Adriaan

+0

"注意:セーフモードが有効になっていると、PHPは実行中のスクリプトと同じUID(所有者)を持つディレクトリがあるかどうかを確認します。 http://php.net/manual/en/function.mkdir.phpまた、pclzipのライブラリを試すことができます – mikakun

+0

あなたの返事をありがとう。しかし、私のすべてのdirsに所有者UIDがあります。 – Adriaan

関連する問題