PHP create in array with foreach function - php

i try to puth the files path in a mail for download it without ftp
$nomecognome = $_POST['cname'];
$email = $_POST['cemail'];
$biography = $_POST['ctext'];
$datanascita = $_POST['cdata'];
$controllo = $_POST['ccontrol'];
$files = $_FILES['uploader'];
if(empty($controllo)){
i clear the name
$accenti = array( 'à' , 'è' , 'é' , 'ì' , 'ò' , 'ù' , '\'' , ' ' );
$noaccenti = array( 'a' , 'e' , 'e' , 'i' , 'o' , 'u' , '_' , '_' );
$author = strtolower( str_replace( $accenti , $noaccenti , $nomecognome ) );
create an array for future comparison file ext
$allowedExtensions = array('jpg', 'jpeg', 'png', 'bmp', 'tiff', 'gif', 'pdf');
control if the form is empty
if ( !empty ( $files ) ){
start uploading images
foreach ($files['name'] as $key => $value) {
if(is_uploaded_file($files['tmp_name'][$key]) && $files['error'][$key] == 0) {
Create an unique name for the file using the name of user and random number plus filename
$filename = $files['name'][$key];
$filename = $author.rand(0,99).$filename;
//array_push($path, 'http://www.magic-promo.it/uploads/'.$author.$value);
Check if the file was moved
if( move_uploaded_file( $files['tmp_name'][$key], 'uploads/'. $filename) ) {
here i want to create the array ith each image path to send mail
foreach ($filename as $filenames) {
$pathfile[] = array($filename);
}
$body = implode(',', $pathfile);
echo $body; //to view the list of path
}
else{
echo move_uploaded_file($files['tmp_name'][$key], 'uploads/'. $filename);
echo 'file non uploadato';
}
}
else {
echo 'The file was not uploaded.';
}
}
i
thanks for the help!

Try this:
$pathfile = array(); // prevent undefined variable notice
if ( !empty ( $files ) ) {
foreach ($files['name'] as $key => $value) {
if(is_uploaded_file($files['tmp_name'][$key]) && $files['error'][$key] == 0) {
// Create an unique name for the file using the name of user and random number plus filename
$filename = $files['name'][$key];
$filename = $author.rand(0,99).$filename;
//array_push($path, 'http://www.magic-promo.it/uploads/'.$author.$value);
//Check if the file was moved
if( move_uploaded_file( $files['tmp_name'][$key], 'uploads/'. $filename) ) {
$pathfile[] = $filename;
} else {
echo move_uploaded_file($files['tmp_name'][$key], 'uploads/'. $filename);
echo 'file non uploadato';
}
} else {
echo 'The file was not uploaded.';
}
}
}
$body = implode(',', $pathfile); // $body = implode("\n", $pathfile);
echo $body; //to view the list of path

Related

How write arrays name while parsing an ini file in PHP?

I am using parse_ini_file to parse an ini file using PHP.
Now I first upload an INI file to my server then open it and allow user to mak custom changes to the file.
Now once users has edited the file i get the data from post and save the file back to server. But Now i dont get my sections. INIdetails,Dialplan in the updated file so when i also want to write that to file how to do that?
This is the code :
$this->data['parameters'] = parse_ini_file($path.$filename,true);
/*Getting Our POST DATA from View*/
$data = array(
'SipUserName' => $this->input->post('SipUserName') ,
'SipAuthName' => $this->input->post('SipAuthName'),
'DisplayName' => $this->input->post('DisplayName'),
'Password' => $this->input->post('Password'),
'Domain' => $this->input->post('Domain'),
'Proxy' => $this->input->post('Proxy'),
'Port' => $this->input->post('Port'),
'ServerMode' => $this->input->post('ServerMode'),
'Param_1' => $this->input->post('Param_1'),
'Param_2' => $this->input->post('Param_2')
);
/*Creating New file with the name of customer loggedin*/
$name = $this->session->userdata('username');
$ext = $this->session->userdata('extension');
$custom_file = fopen('uploads/'.$name.'_'.$ext.'.ini', 'w');
fwrite($custom_file, "[INIDetails]\n");
foreach ($data as $key => $value)
{
fwrite($custom_file, " $key = $value\n");
}
fclose($custom_file);
/*Setting path to New CUSTOM file with customer name as prefix*/
$file = $path.$custom_file;
function write_php_ini($array, $file)
{
$res = array();
foreach($array as $key => $val)
{
if(is_array($val))
{
$res[] = "[$key]";
foreach($val as $skey => $sval) $res[] = "$skey = ".(is_numeric($sval) ? $sval : '"'.$sval.'"');
}
else $res[] = "$key = ".(is_numeric($val) ? $val : '"'.$val.'"');
}
safefilerewrite($file, implode("\r\n", $res));
}
function safefilerewrite($fileName, $dataToSave)
{ if ($fp = fopen($fileName, 'w'))
{
$startTime = microtime(TRUE);
do
{ $canWrite = flock($fp, LOCK_EX);
// If lock not obtained sleep for 0 - 100 milliseconds, to avoid collision and CPU load
if(!$canWrite) usleep(round(rand(0, 100)*1000));
} while ((!$canWrite)and((microtime(TRUE)-$startTime) < 5));
//file was locked so now we can store information
if ($canWrite)
{ fwrite($fp, $dataToSave);
flock($fp, LOCK_UN);
}
fclose($fp);
}
}
/*Creates ini file, dumps array to string and creates .INI file*/
write_php_ini($data,$file);
The culprit from your previous code is that your array is not formatted correctly, it should be array of arrays to achieve what you want.
Try below code:
// First read the ini file that the user was editing
// Your idea to read the existing ini file is good, since it will generate you the structured array
$previous_data = parse_ini_file($path . $filename, true);
// Overwrite/edit the previous_data using user's post data
$previous_data['INIDetails']['SipUserName'] = $this->input->post('SipUserName');
$previous_data['INIDetails']['Password'] = $this->input->post('Password');
$previous_data['INIDetails']['Domain'] = $this->input->post('Domain');
$previous_data['INIDetails']['Proxy'] = $this->input->post('Proxy');
$previous_data['INIDetails']['Port'] = $this->input->post('Port');
$previous_data['INIDetails']['SipAuthName'] = $this->input->post('SipAuthName');
$previous_data['INIDetails']['DisplayName'] = $this->input->post('DisplayName');
$previous_data['INIDetails']['ServerMode'] = $this->input->post('ServerMode');
$previous_data['INIDetails']['UCServer'] = $this->input->post('UCServer');
$previous_data['INIDetails']['UCUserName'] = $this->input->post('UCUserName');
$previous_data['INIDetails']['UCPassword'] = $this->input->post('UCPassword');
$previous_data['DialPlan']['DP_Exception'] = $this->input->post('DP_Exception');
$previous_data['DialPlan']['DP_Rule1'] = $this->input->post('DP_Rule1');
$previous_data['DialPlan']['DP_Rule2'] = $this->input->post('DP_Rule2');
$previous_data['DialPlan']['OperationMode'] = $this->input->post('OperationMode');
$previous_data['DialPlan']['MutePkey'] = $this->input->post('MutePkey');
$previous_data['DialPlan']['Codec'] = $this->input->post('Codec');
$previous_data['DialPlan']['PTime'] = $this->input->post('PTime');
$previous_data['DialPlan']['AudioMode'] = $this->input->post('AudioMode');
$previous_data['DialPlan']['SoftwareAEC'] = $this->input->post('SoftwareAEC');
$previous_data['DialPlan']['EchoTailLength'] = $this->input->post('EchoTailLength');
$previous_data['DialPlan']['PlaybackBuffer'] = $this->input->post('PlaybackBuffer');
$previous_data['DialPlan']['CaptureBuffer'] = $this->input->post('CaptureBuffer');
$previous_data['DialPlan']['JBPrefetchDelay'] = $this->input->post('JBPrefetchDelay');
$previous_data['DialPlan']['JBMaxDelay'] = $this->input->post('JBMaxDelay');
$previous_data['DialPlan']['SipToS'] = $this->input->post('SipToS');
$previous_data['DialPlan']['RTPToS'] = $this->input->post('RTPToS');
$previous_data['DialPlan']['LogLevel'] = $this->input->post('LogLevel');
// Set Name of New file with the name of customer logged in
$name = $this->session->userdata('username');
$ext = $this->session->userdata('extension');
$custom_file = "$name_$ext.ini";
$new_filename = $path . $custom_file;
// Write the INI file
write_php_ini($data, $new_filename);
function write_php_ini($array, $new_filename)
{
$res = array();
foreach ($array as $key => $val) {
if (is_array($val)) {
$res[] = "[$key]";
foreach ($val as $skey => $sval) {
$res[] = "$skey = " . (is_numeric($sval) ? $sval : '"' . $sval . '"');
}
} else {
$res[] = "$key = " . (is_numeric($val) ? $val : '"' . $val . '"');
}
}
safefilerewrite($new_filename, implode("\r\n", $res));
}
function safefilerewrite($new_filename, $dataToSave)
{
if ($fp = fopen($new_filename, 'w')) {
$startTime = microtime(true);
do {
$canWrite = flock($fp, LOCK_EX);
// If lock not obtained sleep for 0 - 100 milliseconds, to avoid collision and CPU load
if (!$canWrite) {
usleep(round(rand(0, 100) * 1000));
}
} while ((!$canWrite) and ((microtime(true) - $startTime) < 5));
//file was locked so now we can store information
if ($canWrite) {
fwrite($fp, $dataToSave);
flock($fp, LOCK_UN);
}
fclose($fp);
}
}
From your previous code, there are bunch of inappropriate codes which I remove. Also, too many inconsistencies like Method naming, variable naming etc.
If your function was named Camel cased then through out your code it must be named as camel-cased. If your variables are with underscore, then through out your code, they must have underscore for two or more worded variable.
I didn't edit the naming convention of your code so you won't be confused but i suggest to have a consistent naming convention through out your project.
UPDATED:
based on your answer, it seems like you changed your whole code. I would like to provide another way using nested foreach and passing by reference that save couple of lines:
$this->data['params'] = $this->parameter_m->get();
$this->data['parameters'] = parse_ini_file($path . $filename, true);
foreach ($this->data['parameters'] as $key_header => &$value_header) {
foreach ($value_header as $key_item => &$value_item) {
$value_item = $this->input->post($key_item);
}
}
$this->load->helper('file');
$this->load->library('ini');
$file = $path . $filename;
$ini = new INI($file);
$ini->read($file);
$ini->write($file, $this->data['parameters']);
Finally i got an answer:
I will loop through what i get from POST and will get each array's key. And then i will give the resultant to my Write Method
$this->data['params'] = $this->parameter_m->get();
/*Getting the parameters to display on view*/
$this->data['parameters'] = parse_ini_file($path.$filename,true);
while (current($this->data['parameters']) )
{
$param_set = current($this->data['parameters']);
$param_type = key($this->data['parameters']);
foreach ($param_set as $key =>$value)
{
$this->data['parameters'][$param_type][$key] = $this->input->post($key);
}
next($this->data['parameters']);
}
$this->load->helper('file');
$this->load->library('ini');
$file = $path.$filename;
$ini = new INI($file);
$ini->read($file);
$ini->write($file, $this->data['parameters']);

Change auto generated filename

So I have this PHP-script which is linked to a contact form on one of my clients sites. What the PHP-script does is basically to take all the fields and values and generates a .txt file with the information given in the form. After that the script decides what to name the file depending on what information you add in the field 'forefternamn'.
My question is pretty straight forward. How do I, instead of naming the file after the field, name it with a serial number?
I would like the serial number to start with 1000 on the first submit, then I would just like it to keep going with 1001, 1002, 1003, 1004, etc..
So the files would look like this:
1000.txt
1001.txt
1002.txt
1003.txt
1004.txt
etc...
This is the PHP-script I'm using:
<?php
$nyckel = array(
'privatforetag',
'forefternamn',
'foretagsnamn',
'gatuadress',
'mobil',
'telefon',
'e-post',
'epost',
'kalender-tidig',
'kalender-senast',
'flyttar-fran-gatuadress',
'flyttar-fran-portkod',
'flyttar-fran-postadress',
'boendetyp',
'meter',
'hiss',
'flyttar-till-gatuadress',
'flyttar-till-postnummer',
'flyttar-till-portkod',
'boendetyp2',
'meter2',
'hiss2',
'rum',
'personer',
'kontor',
'moblerat',
'boyta',
'biyta',
'inventarielista',
'packning',
'uppackning',
'inventarie',
'antalflyttlador',
'flyttlada',
'miljostation',
'flyttstad',
'magasinering',
'student',
'ovriginfo',
'rekommenderad',
'hurhittade'
);
foreach ($nyckel as $key) {
if ($_POST[$key]) {
$input .= $_POST[$key]. "\t";
} else {
$input .= "-\t";
}
$thekey .= $key. "\t";
}
$index = 1;
$name = str_replace(" " , "" , $_POST["forefternamn"]);
$filename = $name . $index . ".txt";
while (file_exists($filename)) {
$index++;
$filename = $name .$index. ".txt";
}
$handle = fopen($filename, 'w') or die('Cannot open file: '.$filename);
fwrite($handle, $thekey);
fwrite($handle, PHP_EOL);
fwrite($handle, $input);
fclose($handle);
if (file_exists($filename)) {
echo "<script>window.location = 'www.domain.com'</script>";
}?>
Create a database table with a primary key ID field and a name field. Insert the name into the table and retrieve the generated ID. Use this generated ID to name the file.
How about something like:-
$dir='c:/temp/inv/';
$col=glob( $dir . '*.txt' );
$tmp=array();
foreach( $col as $file ) $tmp[]=intval( pathinfo( $file, PATHINFO_FILENAME ) );
$last=intval( max( $tmp ) );
$next=( $last+1 ).'.txt';
echo $next;
To continue the idea as requested, and to keep in line with the 'racy' ? comment... how about:-
$nyckel = array(
'privatforetag',
'forefternamn',
'foretagsnamn',
/* rest of the elements removed for brevity */
'student',
'ovriginfo',
'rekommenderad',
'hurhittade'
);
/* Helper function to generate next invoice id */
function getnextinvid( $path=false ){
if( $path ){
$col=glob( $path . '*.txt' );
$tmp=array();
foreach( $col as $file ) $tmp[]=intval( pathinfo( $file, PATHINFO_FILENAME ) );
$last=intval( max( $tmp ) );
return $path . ( $last+1 ).'.txt';
}
return false;
}
/* Loop through array to get POSTed values */
$thekey=$input=array();
foreach( $nyckel as $key ) {
$input[]=isset( $_POST[ $key ] ) ? $_POST[ $key ] : '-';
$thekey[]=$key;
}
/* The output filename / invoice should be sequentially numbered */
$output=getnextinvid('c:/temp/inv/');
if( $output ) {
/* write your data to the output file */
$bytes=file_put_contents( $output, implode( "\t", $thekey ) . PHP_EOL . implode( "\t", $input ), FILE_TEXT );
/* Does the new invoice exist? */
$exists=file_exists( $output ) ? true : false;
/* good practice to call this after calls to certain functions */
clearstatcache();
/* Redirect - alternatively use javascript as originally */
if( $exists && $bytes ) header('location: http://www.domain.com');
} else {
echo 'failed';
}

how to remove pathinfo error from php page?

i make a template in which i select multiple files and i make php page in which i upload the files but when i upload the files it gives me error like
Warning: pathinfo() expects parameter 1 to be string, array given in C:\xampp\htdocs\jobboard\system\user-scripts\classifieds\apply_now.php on line 67
here is my code:
<input type="file" name="file_tmp[]" multiple />
and here is my apply_now.php:
if (!empty($_FILES['file_tmp']['name'])){
$fileFormats = explode(',',SJB_System::getSettingByName('file_valid_types'));
foreach ( $_FILES['file_tmp']['name'] as $file ) {
$fileInfo = pathinfo($file);
if ( !in_array(strtolower($fileInfo['extension']), $fileFormats) ) {
$errors['NOT_SUPPORTED_FILE_FORMAT'] = strtolower($fileInfo['extension']) . ' ' . SJB_I18N::getInstance()->gettext(null, 'is not in an acceptable file format');
}
}
}
The error is caused by the fact that you are giving an array as argument, instead of a string, just as the error message tells you.
This can be fixed by changing your foreach code to the following:
foreach ( $_FILES['file_tmp']['name'] as $key => $file ) {
$fileInfo = pathinfo($_FILES['file_tmp']['name'][$key]);
if ( !in_array(strtolower($fileInfo['extension']), $fileFormats) ) {
$errors['NOT_SUPPORTED_FILE_FORMAT'] = strtolower($fileInfo['extension']) . ' ' . SJB_I18N::getInstance()->gettext(null, 'is not in an acceptable file format');
}
}
Please also refer to my code in my answer on your previous question: https://stackoverflow.com/a/22355746/2539335
If you upload many files then $_FILES['file_tmp']['name'] will be an array.
You should make foreach loop
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "data/$name");
}
}
In you code replace:
$fileInfo = pathinfo($_FILES['file_tmp']['name']);
if ( !in_array(strtolower($fileInfo['extension']), $fileFormats) ) {
$errors['NOT_SUPPORTED_FILE_FORMAT'] = strtolower($fileInfo['extension']) . ' ' . SJB_I18N::getInstance()->gettext(null, 'is not in an acceptable file format');
}
With:
foreach ( $_FILES['file_tmp']['name'] as $file ) {
$fileInfo = pathinfo($file);
if ( !in_array(strtolower($fileInfo['extension']), $fileFormats) ) {
$errors['NOT_SUPPORTED_FILE_FORMAT'] = strtolower($fileInfo['extension']) . ' ' . SJB_I18N::getInstance()->gettext(null, 'is not in an acceptable file format');
}
}

Codeigniter 2.1 - image name is not working properly on upload

I am using this function to upload images, and it is working except in one part. Where there are more then one image for upload, all images get their name from the first image (overwrite is set to off, so CI is adding number at the end of the name). How can I solve this problem?
function img_upload($folder) {
$this->path = './public/img/' . $folder;
$imgs = array();
$count = 0;
foreach($_FILES as $key => $value):
$img_name = is_array($value['name']) ? $value['name'][$count] : $value['name'];
$img_name = $this->char_replace($img_name, '_');
$count++;
$config = array(
'allowed_types' => 'jpg|jpeg|png|gif',
'upload_path' => $this->path,
'file_name' => $img_name
);
$this->CI->load->library('image_lib');
$this->CI->image_lib->clear();
$this->CI->load->library('upload', $config);
if($key != 'logo'):
if (!$this->CI->upload->do_upload($key)) {
} else {
$image = $this->CI->upload->data();
$imgs[] = $image['file_name'];
}
endif;
endforeach;
if(empty($imgs)):
return FALSE;
else:
return implode(',', $imgs);
endif;
}
Function char_replace is working without a problem.
function char_replace($text, $rep_simbol = " ")
{
$char = array('!', '&', '?', '/', '/\/', ':', ';', '#', '<', '>', '=', '^', '#', '~', '`', '[', ']', '{', '}');
return $name = str_replace($char, $rep_simbol, $text);
}
$this->CI->upload->do_upload($key) expects $_FILES['key'] to only contain one file.
What you can do is, make a copy of $_FILES, loop through it, and for each file set the values of $_FILES['key'].
function img_upload($folder) {
$this->path = './public/img/' . $folder;
$imgs = array();
// Copy of $_FILES
$thisFiles = $_FILES;
// Loop through copy of $_FILES
foreach($theFiles as $key => &$value){
// Create the $_FILES array for each individual file,
// so that do_upload can read it correctly
if(!is_array($value['name'])){
// If it's not an array, make it one,
// this will make our future code easier
foreach($value as $kv => &$val){
$val = array($val);
}
}
// Loop through each file and upload each one
foreach($value['name'] as $count=>$img_name){
$img_name = $this->char_replace($img_name, '_');
foreach($_FILES[$key] as $k => &$v){
// CodeIgniter will think this is the $_FILES array
$v = $theFiles[$key][$k][$count];
}
$config = array(
'allowed_types' => 'jpg|jpeg|png|gif',
'upload_path' => $this->path,
'file_name' => $img_name
);
$this->CI->load->library('image_lib');
$this->CI->image_lib->clear();
$this->CI->load->library('upload', $config);
if($key != 'logo'){
if (!$this->CI->upload->do_upload($key)) {
}
else {
$image = $this->CI->upload->data();
$imgs[] = $image['file_name'];
}
}
}
}
return !empty($imgs) ? implode(',', $imgs) : FALSE;
}
NOTE: This is untested.

Using same function 2nd time

I'm declaring the following function to get a random image from a directory. I want to use the same function in same code to get random image from a different directory. Now the problem is that I have to change $path but I have already used it in declaration while i want to use the different path when I use the function 2nd time
Declaration part
function getImagesFromDir($path) {
$images = array();
if ( $img_dir = #opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// checks for gif, jpg, png
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[] = $img_file;
}
}
closedir($img_dir);
}
return $images;
}
I use it this way 1st time
$root = '';
$path = 'frames/';
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
How shud i use it 2nd time so that it chooses image from different directory.
Probably you don't call the function twice, but declare it twice.
declaration:
function getImagesFromDir($path) {
$images = array();
if ( $img_dir = #opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// checks for gif, jpg, png
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[] = $img_file;
}
}
closedir($img_dir);
}
return $images;
}
calls:
$images1 = getImagesFromDir("/var/www/images1");
$images2 = getImagesFromDir("/var/www/images2");
if you write
function getImagesFromDir($path) ....
again anywhere it gets redeclared and PHP don't supports this
this also happens if you require the file, which declares the function, more than once.
-- edit ---
$strRoot = '';
$astrImages = array();
$astrImages[] = array( 'path' => 'frames/', 'image' => '' );
$astrImages[] = array( 'path' => 'etc1/', 'image' => '' );
$astrImages[] = array( 'path' => 'etc2/', 'image' => '' );
$astrImages[] = array( 'path' => 'etc3/', 'image' => '' );
foreach( $astrImages as $nIndex => $astrImage )
{
$imgList = getImagesFromDir($strRoot . $astrImage['path']);
$astrImages[$nIndex]['image'] = getRandomFromArray($imgList);
}

Categories