razstec Posted March 15, 2012 Share Posted March 15, 2012 Hi everyone, first time in this forum, been using your website for long. I make everything in asp but im having problems with the ftp upload so i decided to use php, using w3school iv came arround with this, the upload it self works great but now im having some problems with the validations. i need it to only upload zip files, check if file already exists, rename file and save data into db. this is what iv come accross, can someone help? <html><head></head><body> <?phpini_set('display_errors', 1);ini_set('log_errors', 1);ini_set('error_log', dirname(__FILE__) . 'error_log.txt');error_reporting(E_ALL);set_time_limit(0);if( $_SERVER['REQUEST_METHOD']=='POST' ){$stud=(estudo);$exam=(exame);$center=(centro);$pat=(pat);$visit=(visit);$data = date (“j/m/Y”);$path1=(upload);$path2=(xpto);$arquivo = $_FILES[‘arquivo’]; if ($_FILES["arquivo"]=="") echo "<script>alert('File is empty');</script>"; if (substr($arquivo, -4) == ".zip" || $arquivo['type'] == "multipart/x-zip" || $arquivo['type']== "application/zip" || $arquivo['type'] == "application/x-zip-compressed" || $arquivo['type'] == "application/x-compressed"){ if ($arquivo['size']>500000000) { exit('Arquivo muito grande. Tamanho máximo permitido 500MB. O arquivo enviado contém '.round($arquivo['size']/1024).'kb'); } $novonome = $stud.$exam.$center.$pat.$visit.$data.'.zip'; $dir = "upload/$path2"; /** * $_GET['stud']'/'$_GET['pat'] */ if (!file_exists($dir)) { mkdir($dir, 0755); } var_dump( $_FILES ); //apenas para debug retirar no fim $servidor = '72.29.75.95'; $caminho = $dir.$novonome; $arquivo = $_FILES['arquivo']; $con_id = ftp_connect($servidor) or die( 'No conectou em: '.$servidor ); ftp_login( $con_id, 'user', '1234' ); ftp_put( $con_id, $caminho.$arquivo['name'], $arquivo['tmp_name'], FTP_BINARY );}else{ echo "<script>alert('Only Zip files are allowed');</script>";}?> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="arquivo" /> <input type="submit" name="enviar" value="Enviar" /> </form></body></html> thanks Link to comment Share on other sites More sharing options...
thescientist Posted March 15, 2012 Share Posted March 15, 2012 what's the problem? Link to comment Share on other sites More sharing options...
razstec Posted March 16, 2012 Author Share Posted March 16, 2012 the script is not working, it say it cant create folder to upload file. heres that part: <?php ini_set('display_errors', 1);ini_set('log_errors', 1);ini_set('error_log', dirname(__FILE__) . 'error_log.txt');error_reporting(E_ALL); set_time_limit(0);if( $_SERVER['REQUEST_METHOD']=='POST' ){ $stud=(estudo);$exam=(exame);$center=(centro);$pat=(pat);$visit=(visit);$data = date("d/m/Y");$path1=(upload);$path2=(xpto); $arquivo = $_FILES['arquivo']; if ($_FILES["arquivo"]=="") echo "<script>alert('File is empty');</script>"; if (substr($arquivo, -4) == ".zip" || $arquivo['type'] == "multipart/x-zip" || $arquivo['type']== "application/zip" || $arquivo['type'] == "application/x-zip-compressed" || $arquivo['type'] == "application/x-compressed") { if ($arquivo['size']>500000000) { exit('Arquivo muito grande. Tamanho máximo permitido 500MB. O arquivo enviado contém '.round($arquivo['size']/1024).'kb'); /** * echo "<script>alert('Arquivo muito grande. Tamanho máximo permitido 500MB. O arquivo enviado contém '.round($arquivo['size']/1024).'kb');</script>"; */ $novonome = $data.'_'.$stud.'_'.$exam.'_'.$center.'_'.$pat.'_'.$visit.'.zip'; $dir = "upload/".$stud."/".$pat."/"; var_dump( $_FILES );//apenas para debug retirar no fim $servidor = '22.22.22.22'; $caminho = $dir.$novonome; $arquivo = $_FILES['arquivo']; $con_id = ftp_connect($servidor) or die( 'No Connection at: '.$servidor ); ftp_login( $con_id, 'user', '1234' ); if (file_exists($dir)) { } else { if (ftp_mkdir($con_id, $dir)) { echo "successfully created $dir\n"; } else { echo "There was a problem while creating $dir\n"; } } if (file_exists($caminho )) { echo "<script>alert('That file already exist!');</script>"; } else { ftp_put( $con_id, $caminho, $arquivo['tmp_name'], FTP_BINARY ); ftp_close($con); }}else{echo "<script>alert('Only Zip files are allowed');</script>";}}}?> you can test it here: http://www.aquasplen...m/corc/ftp1.php Link to comment Share on other sites More sharing options...
justsomeguy Posted March 16, 2012 Share Posted March 16, 2012 That might mean that the FTP user doesn't have permission to create a directory there. You may also need to create directories one at a time. Create the upload directory first, then upload/estud, then upload/estud/pat. Also, keep an eye on your error log, there should be a couple warnings or notices listed there. If the error log is empty then make sure PHP has permission to write to it, because you should be seeing error messages there with the current code. Link to comment Share on other sites More sharing options...
razstec Posted March 20, 2012 Author Share Posted March 20, 2012 the ftp user have all permissions tryed like this but still dont work... theres something missing... $conn = ftp_connect($host) or fail('Cannot connect.'); ftp_login($conn, $user, $pass) or fail('Cannot login.'); if (!in_array('estudo', ftp_nlist($conn, 'Uploads'))) { ftp_mkdir($conn, 'Uploads/estudo') or fail('Cannot create dir estudo.'); } if (!in_array('pat', ftp_nlist($conn, 'Uploads/estudo'))) { ftp_mkdir($conn, 'Uploads/estudo/pat') or fail('Cannot create dir pat.'); } Link to comment Share on other sites More sharing options...
thescientist Posted March 20, 2012 Share Posted March 20, 2012 which part does it fail in that code? That would be where I would start looking. Link to comment Share on other sites More sharing options...
razstec Posted March 20, 2012 Author Share Posted March 20, 2012 hi, i really dont know now i get a 500error and the log dont show any error with the page. manage to clean and simplify the code, maybe you can see anything wrong? because i can, like i said its my first php scrip <html><head></head><body><?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);$stud=(estudo);$exam=(exame);$center=(centro);$pat=(pat);$visit=(visit);$data = date("d/m/Y");$path2=(xpto);$out = array('error'=>null);$arquivo =$_FILES['arquivo'];$novonome =$data.'_'.$stud.'_'.$exam.'_'.$center.'_'.$pat.'_'.$visit.'.zip';$dir =$stud."/".$pat."/";$servidor ='22.22.22.22';$caminho =$dir.$novonome;$user='user';$pass='1234':$conn_id = ftp_connect($servidor) or die( 'No Connection at CorcServer, please contact Corc Support at support@corc.pt'); 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!"; } var_dump( $_FILES ); //apenas para debug retirar no fim ftp_login( $conn_id, $user, $pass ); if (!in_array($estudo, ftp_nlist($conn_id, ''))) { ftp_mkdir($conn_id, $estudo) or fail ($out['error'][] ="Cannot create dir $estudo."); } if (!in_array($pat, ftp_nlist($conn_id, $estudo))) { ftp_mkdir($conn_id, $estudo.'/'.$pat) or fail ($out['error'][] ="Cannot create dir $pat."); }if (file_exists($caminho)) { $out['error'][] = "That file already exist"; } else { if (count($out['error'])=0) { ftp_put( $conn_id, $caminho, $arquivo['tmp_name'], FTP_BINARY); ftp_close($conn_id); } if (count($out['error'])>0) {foreach ($file['error'] as $msg){$message .= '<p>'.$msg.'</p>';echo $message;}}else{echo "File uploaded!";}}?> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="arquivo" /> <input type="submit" name="enviar" value="Enviar" /> </form></body></html> thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted March 20, 2012 Share Posted March 20, 2012 You can use an online syntax checker like this one: http://www.piliapp.com/php-syntax-check/ You have an error on the line that sets $pass, there is a colon instead of a semicolon. There are several other errors on that page, you should be seeing several error messages when you run it. Review the tutorials to see how to get form data, these lines are not correct: $stud=(estudo); Link to comment Share on other sites More sharing options...
razstec Posted March 20, 2012 Author Share Posted March 20, 2012 thanks it helped alot its working now thanks for this tool. now i can run and i get these :s Link to comment Share on other sites More sharing options...
razstec Posted March 20, 2012 Author Share Posted March 20, 2012 solve most of all, now only get this array(1) { ["arquivo"]=> array(5) { ["name"]=> string(7) "1MB.zip" ["type"]=> string(28) "application/x-zip-compressed" ["tmp_name"]=> string(27) "C:\Windows\Temp\phpA765.tmp" ["error"]=> int(0) ["size"]=> int(1212489) } }Warning: ftp_put() [function.ftp-put]: estudo/pat/20/03/2012_estudo_exame_centro_pat_visit.zip: The system cannot find the path specified. in C:\inetpub\vhost\cccm\httpdocs\cc\ftp1.php on line 71 cant understand why it dont create dir, it has all permissions Link to comment Share on other sites More sharing options...
justsomeguy Posted March 20, 2012 Share Posted March 20, 2012 The first 6 notices refer to these lines: $stud=(estudo);$exam=(exame);$center=(centro);$pat=(pat);$visit=(visit);$path2=(xpto); What are you trying to do there? The next 2 notices say that you're trying to use a variable called $estudo which is not defined, which is true, you don't define that variable anywhere before you try to use it. The error about ftp_mkdir is caused because $estudo does not exist, you're not telling it what directory to create. The last error is because you're trying to run a function called fail which is not defined. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 20, 2012 Share Posted March 20, 2012 I assume the error from post 10 is because one of those subdirectories doesn't exist, you need to create all subdirectories before uploading a file. It won't automatically create subdirectories that don't exist. Link to comment Share on other sites More sharing options...
razstec Posted March 20, 2012 Author Share Posted March 20, 2012 iv fixt it, i toke those in the beginning... it was just something as simple as this, i define the file with a new name and that name start with date but i forgot and define it as d/m/Y just change it to d_m_Y it was assuming the / as a dir... thanks so much i always get stuck in this type of errors :s thanks again Link to comment Share on other sites More sharing options...
thescientist Posted March 20, 2012 Share Posted March 20, 2012 it's seem like your issue now is with the path, not permissions. have you double checked the path? Link to comment Share on other sites More sharing options...
razstec Posted March 21, 2012 Author Share Posted March 21, 2012 now the only thing that dont work is checking if the file already exists, it just dont do nothing if (file_exists($caminho)) { $out['error'][] = "That file already exist"; } Isnt this right? Link to comment Share on other sites More sharing options...
thescientist Posted March 21, 2012 Share Posted March 21, 2012 I don't get what you are trying to say. Are you saying it always says the file already exists? Or that it says it exists when it doesn't? Feel free to elaborate on what specific problem is, and provide that value of $caminho, so we know it is actually a filename Link to comment Share on other sites More sharing options...
razstec Posted March 21, 2012 Author Share Posted March 21, 2012 oh, sorry ill try to explain myself more clear. i have this $stud='estud2o';$exam='exame';$center='centro';$pat='pat';$visit='visit';$data = date("d-m-Y");$path2='xpto';$out = array('error'=>null);$arquivo =$_FILES['arquivo'];$novonome =$data.'_'.$stud.'_'.$exam.'_'.$center.'_'.$pat.'_'.$visit.'.zip';$dir =$stud."/".$pat."/";$servidor ='22.22.22.22';$caminho =$dir.$novonome;$user='user';$pass='1234';$conn_id = ftp_connect($servidor) or die( 'No Connection'); if (in_array(ftp_rawlist($conn_id, $dir($novonome)))) { $out['error'][] = "That file $novonome already exist!";} It gives me this error Fatal error: Call to undefined function estud2o/pat/() in C:\inetpub\vhost\aquasplender.com\httpdocs\corc\ftp1.php on line 54 im just trying to check if the file exist in server before i upload it. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 21, 2012 Share Posted March 21, 2012 The error is because of this: $dir($novonome) That's the syntax to call a function, but the value of $dir is not a function name. Link to comment Share on other sites More sharing options...
razstec Posted March 21, 2012 Author Share Posted March 21, 2012 iv change it to if (in_array($novonome, ftp_rawlist($conn_id, $dir))) { $out['error'][] = "That file $novonome already exist!";} but now it dont do nothing Link to comment Share on other sites More sharing options...
justsomeguy Posted March 21, 2012 Share Posted March 21, 2012 Print out $novonome, and use print_r to see the result of ftp_rawlist. Link to comment Share on other sites More sharing options...
razstec Posted March 21, 2012 Author Share Posted March 21, 2012 did like this, echo "<br>";print_r ($dir);echo "<br>";print_r ($novonome);echo "<br>"; and got the result of esdtugio/pdat/21-03-2012_esdtugio_exdame_cednhtro_pdat_vidsit.zip Link to comment Share on other sites More sharing options...
justsomeguy Posted March 21, 2012 Share Posted March 21, 2012 What about the output from ftp_rawlist? If you're trying to use ftp_rawlist to check if a file exists then we need to see the filename and the list of files to see what the problem is. Link to comment Share on other sites More sharing options...
razstec Posted March 22, 2012 Author Share Posted March 22, 2012 im sorry but how can i do thank? i tryed to print_r ftp_rawlist and nothing append Link to comment Share on other sites More sharing options...
justsomeguy Posted March 22, 2012 Share Posted March 22, 2012 print_r(ftp_rawlist($conn_id, $dir)); Link to comment Share on other sites More sharing options...
razstec Posted March 23, 2012 Author Share Posted March 23, 2012 Hi, i think i manage to make it work, the problem is that it gets in cache and when i try to upload another file e repeats the process for the last one :s any idea how to solve this cache thing? thanks Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.