Get full drive path for fopen() function in php -
i have csv file ( sample.csv ), put in flashdisk ( drive l: ). read file update database. try run on localhost, works fine.but if script uploaded internet server, script error. server can not recognize disk drive put file (disk drive l:). here first script before :
if (isset($_post['upload1'])) { $allowed_ext = array('csv'); $file_name = $_files['file']['name']; $file_ext = strtolower(end(explode('.', $file_name))); $file_path = realpath($_files['file']['name']); if(in_array($file_ext, $allowed_ext) === true){ $handle = fopen($file_path.'/'.$_files['file']['name'], "r" ); while (! feof($handle)) { $import=fgets($handle); } fclose($handle); } }
because script not going well, try add few lines determine location of faults, complete script:
if (isset($_post['upload1'])) { $allowed_ext = array('csv'); $file_name = $_files['file']['name']; $file_ext = strtolower(end(explode('.', $file_name))); $file_path = realpath($_files['file']['name']); if(in_array($file_ext, $allowed_ext) === true){ if (!@fopen($file_path.'/'.$_files['file']['name'], 'r')) { echo $file_path.'/'.$_files['file']['name']; var_dump($php_errormsg); }else{ $handle = fopen($file_path.'/'.$_files['file']['name'], "r" ); while (! feof($handle)) { $import=fgets($handle); } fclose($handle); } } }
from second script, know disk drives not known because gives following message: /sample.csv null
/sample.csv output of echo $file_path.'/'.$_files['file']['name'];
null output off var_dump($php_errormsg);
my question how script program read csv file drive l: ( l:/sample.csv ) thank advices.
save csv server
if (in_array($file_ext, $allowed_ext) === true) { move_uploaded_file($_files['file']['tmp_name'], '/your/directory/yourfile.csv'); }
then loop through csv requested
if (($handle = fopen("/your/directory/yourfile.csv", "r")) !== false) { while (($data = fgetcsv($handle, 1000, ",")) !== false) { $fieldone = trim($data[0]); $fieldtwo = trim($data[1]); $fieldthree = trim($data[3]); // stuff } fclose($handle); }
after have done need csv rows delete file
unlink('/your/directory/yourfile.csv');
Comments
Post a Comment