I have a web application that generate some php files for new members in sub-directories, I'm using PHP copy function to do that, but members web pages gives 500 Internal Server Error, and the script is working fine if uploaded by FTP, with the hosting account, or the root account.
I think the problem is with the "apache apache" group, because after uploading files with the "root root" group, files are working fine, please help!
this is the function used to copy the whole files in my directory to users ones :
function smartCopy($source, $dest, $options=array('folderPermission'=>0775,'filePermission'=>0775))
{
$result=false;
if (is_file($source)) {
if ($dest[strlen($dest)-1]=='/') {
if (!file_exists($dest)) {
cmfcDirectory::makeAll($dest,$options['folderPermission'],true);
}
$__dest=$dest."/".basename($source);
} else {
$__dest=$dest;
}
$result=copy($source, $__dest);
chmod($__dest,$options['filePermission']);
} elseif(is_dir($source)) {
if ($dest[strlen($dest)-1]=='/') {
if ($source[strlen($source)-1]=='/') {
//Copy only contents
} else {
//Change parent itself and its contents
$dest=$dest.basename($source);
#mkdir($dest);
chmod($dest,$options['filePermission']);
}
} else {
if ($source[strlen($source)-1]=='/') {
//Copy parent directory with new name and all its content
#mkdir($dest,$options['folderPermission']);
chmod($dest,$options['filePermission']);
} else {
//Copy parent directory with new name and all its content
#mkdir($dest,$options['folderPermission']);
chmod($dest,$options['filePermission']);
}
}
$dirHandle=opendir($source);
while($file=readdir($dirHandle))
{
if($file!="." && $file!="..")
{
if(!is_dir($source."/".$file)) {
$__dest=$dest."/".$file;
} else {
$__dest=$dest."/".$file;
}
//echo "$source/$file ||| $__dest<br />";
$result=smartCopy($source."/".$file, $__dest, $options);
}
}
closedir($dirHandle);
} else {
$result=false;
}
return $result;
}
Notice : there are no problem with permissions.
I used this function to solve my problem :
$oldmask = umask(0);
mkdir("apps/$appid", 0755);
umask($oldmask);
Related
I tried deleting file if its already existing .
But i ended up with no result.
Can any one help me with this!!!
$path_user = '/wp-content/plugins/est_collaboration/Files/'.$send_id.'/';
if (!file_exists($path_user)) {
if (mkdir( $path_user,0777,false )) {
//
}
}
unlink($path_user);
if(move_uploaded_file($file['tmp_name'],$path_user.$path)){
echo "Your File Successfully Uploaded" . "<br>";
}
Organize your code, try this:
$path = 'filename.ext'; // added reference to filename
$path_user = '/wp-content/plugins/est_collaboration/Files/'.$send_id.'/';
// Create the user folder if missing
if (!file_exists($path_user)) {
mkdir( $path_user,0777,false );
}
// If the user file in existing directory already exist, delete it
else if (file_exists($path_user.$path)) {
unlink($path_user.$path);
}
// Create the new file
if(move_uploaded_file($file['tmp_name'],$path_user.$path)) {
echo"Your File Successfully Uploaded"."<br>";
}
Keep in mind that PHP will not recursively delete the directory contents, you should use a function like this one
Maybe you missing else condition ?? And file_name variable :
$file_name = 'sample.jpg';
$path_user = '/wp-content/plugins/est_collaboration/Files/'.$send_id.'/';
if (!file_exists($path_user.$file_name))
{
if (mkdir( $path_user,0777,false )) {
}
} else {
unlink($path_user.$file_name);
}
IS there a file that I can upload onto my website (my website is a website that contains very very very sensitive information) that when navigated to in the browser (www.example.com/script.php) can be executed to delete all files from the folder it is uplaoded into/ I want to be able to delete this information easy as that. Kind of like a Self Destruct Button. MY website is used for testing sensitive equiptment commands for diamond blade cutters. We got almost the perfect cut for a diamond which makes ours very valuable. Our site was hacked and there was nothn gi could do in the backend because the password was changed on us so i want to put a secret file in there that does this.
i would not recommend that as a solution,
why don't you secure the script instead ?
anyway just for the fun of it here is a function that will delete a folder
function deleteAll($directory, $empty = false) {
$t= time(); // you can also use it to delete old files by subtracting from this
if(substr($directory,-1) == "/") {
$directory = substr($directory,0,-1);
}
if(!file_exists($directory) || !is_dir($directory)) {
return false;
} elseif(!is_readable($directory)) {
return false;
} else {
$directoryHandle = opendir($directory);
while ($contents = readdir($directoryHandle)) {
if($contents != '.' && $contents != '..') {
if(filemtime($directory . "/" . $contents) < $t) {
$path = $directory . "/" . $contents;
if(is_dir($path)) {
#deleteAll($path);
} else {
#unlink($path);
}
}
}
}
closedir($directoryHandle);
if($empty == false) {
if(!#rmdir($directory)) {
return false;
}
}
return true;
}
}
(!) be careful when you use it it will DELETE everything you can call it like this deleteAll(".", true);
but this is not a solution look into securing your script
I have an issue with my host (and maybe my internet connection) : i use filezilla to upload several huge .zip on my website (each .zip weighs ca. 500Mo) and the connection closes before the upload's end.
So, i thought i would upload a folder containing my photographies (in that way, filezilla uploads several little files and not ONE HUGE file), and then i will make an archive of these by passing through a php script.
Here is the code i did :
/*
params :
string $nom_archive: archive's name in '.zip' (ex. 'archive.zip', 'test.zip', etc.)
string $adr_dossier: the path of the folder to archive (ex. 'images', '../dossier1/dossier2', etc.)
string $dossier_destination : path of the folder where we will copy/paste the archive (ex. 'images/zip', '../archives', etc.)
DOn't change the params $zip and $dossier_base
*/
function zipper_repertoire_recursif($nom_archive, $adr_dossier, $dossier_destination = '', $zip=null, $dossier_base = '') {
if($zip===null) {
$zip = new ZipArchive();
if($zip->open($nom_archive, ZipArchive::CREATE) !== TRUE) {
return false;
}
}
if(substr($adr_dossier, -1)!='/') {
$adr_dossier .= '/';
}
if($dossier_base=="") {
$dossier_base=$adr_dossier;
}
if(file_exists($adr_dossier)) {
if(#$dossier = opendir($adr_dossier)) {
while(false !== ($fichier = readdir($dossier))) {
if($fichier != '.' && $fichier != '..') {
if(is_dir($adr_dossier.$fichier)) {
$zip->addEmptyDir($adr_dossier.$fichier);
zipper_repertoire_recursif($nom_archive, $adr_dossier.$fichier, $dossier_destination, $zip, $dossier_base);
}
else {
$zip->addFile($adr_dossier.$fichier);
}
}
}
}
}
if($dossier_base==$adr_dossier) {
$zip->close();
if($dossier_destination!='') {
if(substr($dossier_destination, -1)!='/') {
$dossier_destination .= '/';
}
if(rename($nom_archive, $dossier_destination.$nom_archive)) {
return true;
}
else {
return false;
}
}
else {
return true;
}
}
}
And then, i call my function that way :
zipper_repertoire_recursif('Myriam&Fabien_byGFernandez_Originales.zip', './Myriam&Fabien/Photos/originales/photos_originales', './Myriam&Fabien/Photos/originales')
But it won't work, and i don't know why.
I've checked if zip is enabled on my server and it is.
DO you have any idea on my problem?
Maybe I'm using ZipArchive the wrong way, but i can't figure out what i did wrong.
COuld you help me, please?
Thank you very much.
I just wanna ask, what is the code needed in my code in order this warning will not show
Warning: mkdir() [function.mkdir]: File exists in
C:\xampp\htdocs\php-robert\dir\dir.php
also did my program is correct? what i want in my program is, if the folder is not exist, make a folder, if its existing just do nothing.. nothing to show, just nothing
dir.php
<?php
$var = "MyFolder";
$structure = "../../file/rep/$var";
if (!mkdir($structure, 0700)) {
}
else
{
echo"folder created";
}
?>
Try the following:
$folder = "folder_name";
// if folder does not exist or the name is used, just not for a folder
if (!file_exists($folder) || !is_dir($folder)) {
if (mkdir($folder, 0755)) {
echo 'Folder created';
} else {
echo 'Unable to create folder';
}
}
if (!is_dir($structure)) {
mkdir($structure);
}
else
{
echo "folder already exists";
}
if (is_dir($structure) == false and mkdir($structure, 0700) == false)
{
echo "error creating folder";
}
else
{
echo "folder exists or was created";
}
You could also test if a file exists, but it isn't a folder
I'm trying to write a script that will upload the entire contents of a directory stored on my server to other servers via ftp.
I've been reading through the documentation on www.php.net, but can't seem to find a way to upload more then one file at a time.
Is there a way to do this, or is there a script that will index that directory and create an array of files to upload?
Thanks in advance for your help!
Once you have a connection open, uploading the contents of a directory serially is simple:
foreach (glob("/directory/to/upload/*.*") as $filename)
ftp_put($ftp_stream, basename($filename) , $filename, FTP_BINARY);
Uploading all files in parallel would be more difficult.
So, I took #iYETER's code, and wrapped it as a class object.
You can call this code by these lines:
$ftp = new FtpNew("hostname");
$ftpSession = $ftp->login("username", "password");
if (!$ftpSession) die("Failed to connect.");
$errorList = $ftp->send_recursive_directory("/local/dir/", "/remote/dir/");
print_r($errorList);
$ftp->disconnect();
It recursively crawls local dir, and places it on remote dir relative. If it hits any errors, it creates a array hierarchy of every file and their exception code (I only capture 2 so far, if it's another error, it throws it default route for now)
The class this is wrapped into:
<?php
//Thanks for iYETER on http://stackoverflow.com/questions/927341/upload-entire-directory-via-php-ftp
class FtpNew {
private $connectionID;
private $ftpSession = false;
private $blackList = array('.', '..', 'Thumbs.db');
public function __construct($ftpHost = "") {
if ($ftpHost != "") $this->connectionID = ftp_connect($ftpHost);
}
public function __destruct() {
$this->disconnect();
}
public function connect($ftpHost) {
$this->disconnect();
$this->connectionID = ftp_connect($ftpHost);
return $this->connectionID;
}
public function login($ftpUser, $ftpPass) {
if (!$this->connectionID) throw new Exception("Connection not established.", -1);
$this->ftpSession = ftp_login($this->connectionID, $ftpUser, $ftpPass);
return $this->ftpSession;
}
public function disconnect() {
if (isset($this->connectionID)) {
ftp_close($this->connectionID);
unset($this->connectionID);
}
}
public function send_recursive_directory($localPath, $remotePath) {
return $this->recurse_directory($localPath, $localPath, $remotePath);
}
private function recurse_directory($rootPath, $localPath, $remotePath) {
$errorList = array();
if (!is_dir($localPath)) throw new Exception("Invalid directory: $localPath");
chdir($localPath);
$directory = opendir(".");
while ($file = readdir($directory)) {
if (in_array($file, $this->blackList)) continue;
if (is_dir($file)) {
$errorList["$remotePath/$file"] = $this->make_directory("$remotePath/$file");
$errorList[] = $this->recurse_directory($rootPath, "$localPath/$file", "$remotePath/$file");
chdir($localPath);
} else {
$errorList["$remotePath/$file"] = $this->put_file("$localPath/$file", "$remotePath/$file");
}
}
return $errorList;
}
public function make_directory($remotePath) {
$error = "";
try {
ftp_mkdir($this->connectionID, $remotePath);
} catch (Exception $e) {
if ($e->getCode() == 2) $error = $e->getMessage();
}
return $error;
}
public function put_file($localPath, $remotePath) {
$error = "";
try {
ftp_put($this->connectionID, $remotePath, $localPath, FTP_BINARY);
} catch (Exception $e) {
if ($e->getCode() == 2) $error = $e->getMessage();
}
return $error;
}
}
Do it in a loop, iterating through all the files in the folder
$servername = $GLOBALS["servername"];
$ftpUser = $GLOBALS["ftpUser"];
$ftpPass = $GLOBALS["ftpPass"];
$conn_id = ftp_connect($servername) or die("<p style=\"color:red\">Error connecting to $servername </p>");
if(ftp_login($conn_id, $ftpUser, $ftpPass))
{
$dir_handle = #opendir($path) or die("Error opening $path");
while ($file = readdir($dir_handle)) {
ftp_put($conn_id, PATH_TO_REMOTE_FILE, $file)
}
}
I coded it, the script uploads whole folder with it's subfolders and files.
I hope it will help you.
<?php
ob_start();
set_time_limit(0);
//r10.net fatal
$anadizin="uploadedeceginizdizin"; //this is the folder that you want to upload with all subfolder and files of it.
$ftpsunucu="domain.com"; //ftp domain name
$ftpusername="ftpuser"; //ftp user name
$ftppass="ftppass"; //ftp passowrd
$ftpdizin="/public_html"; //ftp main folder
$arkadaslarlabardaydik = ftp_connect($ftpsunucu);
$ictikguldukeglendik = ftp_login($arkadaslarlabardaydik, $ftpusername, $ftppass);
if((!$arkadaslarlabardaydik) || (!$ictikguldukeglendik))
{
echo "cant connect!";
die();
}
function klasoruoku($dizinadi)
{
global $nerdeyiz,$fulldizin,$ftpdizin,$arkadaslarlabardaydik,$ftpdizin;
chdir($dizinadi."\\");
$dizin = opendir(".");
while($bilgi=readdir($dizin))
{
if ($bilgi!='.' and $bilgi!='..' and $bilgi!="Thumbs.db")
{
$tamyol="$dizinadi\\$bilgi";
$lokalkla=str_replace("".$nerdeyiz."\\","",$dizinadi)."";
$lokaldosya="$lokalkla\\$bilgi";
$ftpyeyolla=str_replace(array("\\\\","\\"),array("/","/"),"$ftpdizin\\".str_replace("".$fulldizin."","",$dizinadi)."\\$bilgi");
if(!is_dir($bilgi))
{
$yükleme = ftp_put($arkadaslarlabardaydik, $ftpyeyolla, $tamyol, FTP_BINARY);
if (!$yükleme)
{
echo "$lokaldosya <font color=red>uploaded</font>"; echo "<br>"; fls();
}
else
{
echo "$lokaldosya <font color=green>not uploaded</font>"; echo "<br>"; fls();
}
}
else
{
ftp_mkdir($arkadaslarlabardaydik, $ftpyeyolla);
klasoruoku("$dizinadi\\$bilgi");
chdir($dizinadi."\\");
fls();
}
}
}
closedir ($dizin);
}
function fls()
{
ob_end_flush();
ob_flush();
flush();
ob_start();
}
$nerdeyiz=getcwd();
$fulldizin=$nerdeyiz."\\$anadizin";
klasoruoku($fulldizin);
ftp_close($arkadaslarlabardaydik);
?>
If you want to have multiple files uploaded at once, you'll need to use thread or fork.
I'm not sure of a Thread implentation in PHP, but you should take a look at the PHP SPL and/or PEAR
Edit: Thanks to Frank Farmer to let me know that there was a fork() function in PHP known as pcntl_fork()
You'll also have to get the whole content directory recursively to be able to upload all the file for a given directory.