razstec Posted May 23, 2012 Share Posted May 23, 2012 Hi all, i have this script to upload files to server, it creates 2 folders and then the file, the script works fine but when any of the folders already exist it gives me error. can someone help? heres my script: ftp_login( $conn_id, $user, $pass );if (file_exists($direc)) { } else { if (!in_array($direc, ftp_nlist($conn_id, ''))) { ftp_mkdir($conn_id, $direc) or fail ($out['error'][] ="Cannot create dir $direc.");}}if (file_exists($usr)) { } else { if (!in_array($usr, ftp_nlist($conn_id, $direc))) { ftp_mkdir($conn_id, $direc.'/'.$usr) or fail ($out['error'][] ="Cannot create dir $usr.");}}ftp_put( $conn_id, $caminho, $arquivo['tmp_name'], FTP_BINARY); ftp_close($conn_id); Link to comment Share on other sites More sharing options...
Ingolme Posted May 23, 2012 Share Posted May 23, 2012 You can check if a folder exists before creating it using file_exists(). $folder = 'path/folder/';if(!file_exists($folder)) { mkdir($folder);} Link to comment Share on other sites More sharing options...
razstec Posted May 24, 2012 Author Share Posted May 24, 2012 iv done that, if you see in my script iv done that but it dont work :s Link to comment Share on other sites More sharing options...
Ingolme Posted May 24, 2012 Share Posted May 24, 2012 You should be comparing the exact same path in file_exists() as you are using in the mkdir() function.In your code you're creating the directory $direc.'/'.$usr but using $usr in the file_exists() function. Those are two different paths. You should get rid of the else statements and just negate the condition.This code: if(condition) {} else { // Do something} is equivalent to this: if(!condition) { // Do something} Link to comment Share on other sites More sharing options...
razstec Posted May 24, 2012 Author Share Posted May 24, 2012 (edited) done what you sujested but its the same. its very weird here my full code, maybe you see something im missing <?phpini_set('display_errors', 1);ini_set('log_errors', 1);ini_set('error_log', dirname(__FILE__) . 'error_log.txt');error_reporting(E_ALL);if( $_SERVER['REQUEST_METHOD']=='POST' ){set_time_limit(0);$out = array('error'=>null);$stud =$_GET['stud'];$pat =$_GET['pat'];$novonome =$_GET['novonome'];$arquivo =$_FILES['arquivo'];$dir =$stud."/".$pat."/";$caminho =$novonome;$servidor ='184.163.1.11';$user='user';$pass='1234';$conn_id = ftp_connect($servidor) or die( 'No Connection at Server, please contact <a href="contacts.asp">Support</a>!!'); if ($_FILES["arquivo"]=="") { $out['error'][] = "Please specify a file"; } if ($arquivo['type'] == "multipart/x-zip" || $arquivo['type']== "application/zip" || $arquivo['type'] == "application/x-zip-compressed" || $arquivo['type'] == "application/x-compressed") { } else { $out['error'][] = "Only Zip files are allowed!"; } if ($arquivo['size']>500000000) { $out['error'][] = "File upload limit is 500MB!"; }ftp_login( $conn_id, $user, $pass ); if (count($out['error'])>0) { foreach ($out['error'] as $msg) { $message = '<p>'.$msg.'</p>'; echo $message; echo "<br><form><input type=\"button\" value=\"ERROR!! Please check your file and update again!\" onClick=\"javascript:history.go(-1)\"></form>"; }} else { if(!file_exists($conn_id,$dir)) { if (!in_array($pat, ftp_nlist($conn_id,$stud))) { ftp_mkdir($conn_id,$dir) or fail ($out['error'][] ="Cannot create directory $pat."); }} ftp_put( $conn_id, $caminho, $arquivo['tmp_name'], FTP_BINARY); ftp_close($conn_id); echo '<meta http-equiv="refresh" content="0, email.asp?actnow=gonow&var_mal=ficheir" />';}}?><!doctype html><!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]--><!--[if IE 7 ]> <html lang="en" class="no-js ie7"> <![endif]--><!--[if IE 8 ]> <html lang="en" class="no-js ie8"> <![endif]--><!--[if IE 9 ]> <html lang="en" class="no-js ie9"> <![endif]--><!--[if (gt IE 9)|!(IE)]><!--><html lang="en" class="no-js"> <!--<![endif]--><head><meta charset="UTF-8"> <title></title> <script src="modal/js/libs/jquery.min.js"></script><script src="modal/js/libs/modernizr-1.7.min.js"></script> <link rel="stylesheet" href="modal/css/reset.css" media="all"> <link rel="stylesheet" href="modal/css/style.css" media="all"></head><body><section id="cjModalWindow"><header> <h3>Choose File To Upload</h3> <a href="#" class="xClose"><img src="modal/images/x-close.png" alt="Close" title="Close"></a></header> <article> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="arquivo" /> <input type="submit" name="enviar" value="Send"></form> </article> <footer> <img src="modal/images/expandNotation.png" class="expandNotation" title="Expand Window" alt="Expand Window"> </footer></section><footer id="fMain"></footer></body></html> thanks for the help Edited May 24, 2012 by razstec Link to comment Share on other sites More sharing options...
Ingolme Posted May 24, 2012 Share Posted May 24, 2012 file_exists() only has one parameter, not two as you have here: file_exists($conn_id,$dir) Link to comment Share on other sites More sharing options...
razstec Posted May 24, 2012 Author Share Posted May 24, 2012 should i do like this? something=$conn_id.$dirfile_exists($something) Link to comment Share on other sites More sharing options...
razstec Posted May 24, 2012 Author Share Posted May 24, 2012 that didnt work. how hard can this be?? Link to comment Share on other sites More sharing options...
Err Posted May 24, 2012 Share Posted May 24, 2012 (edited) $file = $dir.$conn_id;if (file_exists($file)) { echo "file does exist";} Try that. If that fails put var_dump($dir,$conn_id); and post the output of that here on the forums. Edited May 24, 2012 by Err 1 Link to comment Share on other sites More sharing options...
razstec Posted May 24, 2012 Author Share Posted May 24, 2012 (edited) nothing. here the result from dump add to change your script to this $file = $conn_id.$dir;if (file_exists($file)) { echo "file does exist";} and the var_dump tovar_dump($conn_id,$dir); resource(1) of type (FTP Buffer) string(19) "EURO/0000000/" Warning: ftp_mkdir(): Directory already exists in C:\inetpub\wwwroot\CORC\ftp.php on line 60 Fatal error: Call to undefined function fail() in C:\inetpub\wwwroot\CORC\ftp.php on line 60 line 60 isftp_mkdir($conn_id,$dir) or fail ($out['error'][] ="Cannot create directory $pat."); Edited May 24, 2012 by razstec Link to comment Share on other sites More sharing options...
Ingolme Posted May 24, 2012 Share Posted May 24, 2012 You don't need $conn_id for anything. That's a resource used for the FTP functions. Don't give it to the file_exists() function. 1 Link to comment Share on other sites More sharing options...
justsomeguy Posted May 24, 2012 Share Posted May 24, 2012 file_exists checks the local filesystem, not an FTP server. You need to download the list of files and directories from the server and put them in an array if the aren't already, and use in_array to check if the name you're looking for is in the array. You may be able to use an HTTP path with file_exists if your server has it enabled, so if you want to do that then you'll need to build the HTTP path to point to the file you're checking and then use file_exists with the fully-qualified path. 1 Link to comment Share on other sites More sharing options...
Ingolme Posted May 24, 2012 Share Posted May 24, 2012 I was supposing that he was trying an FTP connection to his own server. Link to comment Share on other sites More sharing options...
razstec Posted May 25, 2012 Author Share Posted May 25, 2012 it worked thats all Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now