php ftp check if folder exists always return error creating folder
can someone please tell me what I'm doing wrong in this code?
if($id != '') {
if(is_dir("../public_html".$tem_pasta['path']."/pics/".$id)) {
echo "pasta já existia";
$destination_file = "../public_html".$tem_pasta['path']."/pics/".$id."/".$myFileName;
} else {
//pasta nao existia
if (ftp_mkdir($conn_id, "../public_html".$tem_pasta['path']."/pics/".$id)) {
$destination_file = "../public_html".$tem_pasta['path']."/pics/".$id."/".$myFileName;
//echo "pasta criada<br>";
} else {
echo "erro, não criou a pasta<br>开发者_Python百科;";
}
}
} else {
$destination_file = "../public_html".$tem_pasta['path']."/pics/".$myFileName;
}
it checks if I've a folder ($id) within my pics directory, and if not the script creates a new one. works good, but if I try to upload another file to the previous created folder it does return an error, saying it didn't create the folder...
thanks
I don't think you can use is_dir on a FTP resource, what you should do is check if the size of the dir/file is -1 with ftp_size.
Because I think what now happens is: you are trying to make the same folder again, and this is why a error occurs.
Edit: Or check with ftp_chdir!
<?php
function ftp_directory_exists($ftp, $dir)
{
// Get the current working directory
$origin = ftp_pwd($ftp);
// Attempt to change directory, suppress errors
if (@ftp_chdir($ftp, $dir))
{
// If the directory exists, set back to origin
ftp_chdir($ftp, $origin);
return true;
}
// Directory does not exist
return false;
}
?>
Should work!
is_dir
works only on the local file-system. If you want to check if a ftp-directory already exists try this:
function ftp_is_dir($ftp, $dir)
{
$pushd = ftp_pwd($ftp);
if ($pushd !== false && @ftp_chdir($ftp, $dir))
{
ftp_chdir($ftp, $pushd);
return true;
}
return false;
}
if($id != '') {
if(ftp_is_dir($conn_id, "../public_html".$tem_pasta['path']."/pics/".$id)) {
// and so on...
Use ftp_nlist
and in_array
$ftp_files = @ftp_nlist($this->connection, $directory);
if ($ftp_files === false) {
throw new Exception('Unable to list files. Check directory exists: ' . $directory);
}
if (!in_array($directory, $ftp_files)) {
$ftp_mkdir = @ftp_mkdir($this->connection, $directory);
if ($ftp_mkdir === false) {
throw new Exception('Unable to create directory: ' . $directory);
}
}
With PHP 8.0 (on AWS) the solution with @ftp_chdir() does not hide the error and causes the program to stop. The solution with ftp_nlist() doesn't give good results because it returns either false if the path doesn't exist or an element if the path is a file or a list if it's a directory but doesn't give the "." and ".." elements that would identify a directory.
The ftp_mlsd() function seemed more convenient: it returns a list, possibly empty, only if the path is a directory, false otherwise. But it doesn't work correctly on all servers!
Finally it is the ftp_rawlist() function which seems to be the most generalized because it launches a LIST command on the server and returns the result. If the path is a directory we have an array, possibly empty and we have the value false if it is not a directory.
$list = ftp_rawlist( $ftp_conn, $remote_path );
$is_dir = is_array( $list );
精彩评论