How to move multiple upload file in different folder - php

$uploaded = false;
for($i=0;$i
}
$uploaded_cc = false;
for($i=0;$i
}
Please help me.. Above program works well and store multiple files in my folder but i want store different folder. Above program works first FOR LOOP only and Second FOR LOOP con not work. what problem in my second FOR LOOP , Please reply me

For loops typically have a condition in them.
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
http://php.net/manual/en/control-structures.for.php

Related

Display pictures from a folder in the specific way - PHP

I am trying to create a dynamic gallery in php with specific order of pictures on the page, but I can't find the function or piece of php code to do so.
Conditions:
The gallery must be dynamic, pictures will be coming from a folder via php, because when I'll add/upload more pictures in the folder they must be displayed on the page without adding manually the html tags in. ( this part is easy, problem is the condition 2 ).
The first row will have 5 pictures, the second - 4 pictures (important for the specific effect).
My code:
$files = glob("layout/gallery/*.jpg");
rsort($files, SORT_NATURAL);
for ($i=0; $i < count($files); $i++) {
for( ; $i<5; $i++){
$one = $files[$i];
echo '<img src="'.$one.'">' . '<br><br>';
}
echo '<br>';
for( ; $i<9; $i++){
$two = $files[$i];
echo '<img src="'.$two.'">' . '<br><br>';
}
}
The code works well, but it just displays 9 pictures obviously. I was unable to make it dynamic displaying 5 pictures first, 4 pictures after and stay this way in a loop till displays all pictures from that folder.
You can take advantage of the array_splice function that removes elements from the array everytime it returns those elements :
$files = glob("layout/gallery/*.jpg");
rsort($files, SORT_NATURAL);
// split the files in rows
$rows = [];
while(count($files) != 0) {
// even rows have 5 elements, odd ones have 4
$num_files_to_splice = count($rows) % 2 == 0 ? 5 : 4;
$rows[] = array_splice($files, 0, $num_files_to_splice);
}
// display them accordingly
foreach($rows as $row) {
foreach($row as $file) {
echo '<img src="'.$file.'">';
}
echo '<br><br>';
}

Name each new dir with the next available number with php

I'm working on a php file where I want to create one or more directories with names ranging from 1 to 999 or more. I'm creating the first of all directories using the following code:
<?php
$id = '001';
mkdir($id)
?>
What I want to succeed is to automatically create a new directory using as a name the next available number (i.e. 002, 003, 004, 005 etc) either as a string or an integer. However, I really stuck and I try to use:
<?php
$id = 001;
if (file_exists($id)) {
$id = $id + 1;
mkdir($id);
}
?>
..but it doesn't work. Any ideas?
I forgot to mention that the above code is part of the if statement inside the same php code.
Several ways to do this, depending on your use case. This function may work for you:
<?PHP
function makedir($id){
if(file_exists($id)){
$id++;
makedir($id);
}else{
mkdir($id);
return true;
}
}
makedir(1);
This solution of incremented directory names is going to become ugly after a while though; you should probably find a better solution to your problem.
You could do something like this:
// Loop through all numbers.
for ($i = 1; $i <= 999; $i++) {
// Get the formatted dir name (prepending 0s)
$dir = sprintf('%03d', $i);
// If the dir doesn't exist, create it.
if (!file_exists($dir)) {
mkdir($dir);
}
}
Edit: the above was assuming you wanted to make all 999 directories. You could do the following to just append the next available number:
function createDir($dir) {
$newDir = $dir;
$num = 1;
while (file_exists($newDir)) {
$newDir = $dir.sprintf('%03d', $num++);
}
return $newDir;
}

Create list-group(boostrap) by php (Count contact in vcard file)

Example: If My Vcard have a 10 contact. Show 10 list group .
I try for loop : for($i=0; $i<$vcard; $i++)
In html:
In php: I don't know how to create that
I have no idea to do right now. Can you help me?
in html
in php I dont know how to create that
Assuming $vcard is an array you could do
foreach($vcard as $contact) {
// do something with the data of $contact
}
PHP manuael foreach
But you can also stay with your for-loop
for($i=0; $i < count($vcard); $i++) {
// do something with data of $vcard[$i]
}

Adding a number if file exists PHP

I am trying to see if a certain file exists. If it does, I need to make it look like #_filename.jpeg. It works, but only adds a 1 and does not increment further.
This is my code:
if (file_exists('upload/'.$file_name)) {
$i = 1;
while(file_exists('upload/'/$i."_".$file_name)) {
$i++;
}
$file_name = $i."_".$file_name;
}
you have syntax error (missing .) so your while condition not working and not going to loop so $i not increases try to replace
while(file_exists('upload/'/$i."_".$file_name)) {
to
while(file_exists('upload/'.$i."_".$file_name)) {

Get filesize for currentrecord

I dont know how to use filesize($filename) in this particular code that I need, the problem is I do not know how to represent the filename...
Could someone please tell me what should go after $tblDocs->Value["filesize"]=
global $dal;
$tblDocs = $dal->Table("doc_files");
$fileArray = my_json_decode($values["file"]);
for($i = 0; $i < count($fileArray); $i++)
{
$tblDocs->Value["parent_folder_id"]=$_SESSION["current_folder"];
$tblDocs->Value["file_type"]="file";
$tblDocs->Value["file"]=my_json_encode(array($fileArray[$i]));
$tblDocs->Value["hash"]=generatePassword(HASH_LENGTH);
$tblDocs->Value["name"]=$fileArray[$i]["usrName"];
$tblDocs->Value["ownerid"]=$_SESSION["user_id"];
$tblDocs->Value["created"]=now();
$tblDocs->Value["filesize"]=
$tblDocs->Add();
}
UPDATE: I have just exported a csv file form my database to see what is actually stored in the file field, and this is result
[{"usrName":"rootcause.jpg","name":"/home/sites/iso-drive.co.uk/public_html/portal/files/rootcause_48zewwp6.jpg","size":1323,"type":"image/jpeg"}]
So...how Do I capture the information in 3rd field "size":1323 - specifically the 1323?
Boldly guessing
$tblDocs->Value["filesize"]=$fileArray[$i]["size"];

Categories