PHP - Check A Variable Is Not A Folder/directory
if ($d = opendir("uploads")) { while ($file = readdir($d)) { if ($file != "." && $file != ".." ) // if( is_file($file) ) { $files[] = $file; } } closedir($d); }if ($file != "." && $file != ".." $file != ???????? ) just a bit stuck here Similar TutorialsI have a PHP script that reads the image files in a folder and displays them for viewing. When originally tested with three images, it seemed to be displaying them in alphabetical order by name (as desired). Now, with more image files added, it is displaying the pictures in a seemingly random order. Can I use a SORT command similar to the method for organizing data from a database in my script (to organize the files by name)? I have been looking around and have not been able to find any useful information on this. I am trying to transfer files from a folder in the directory to a table in a database. Eventually they will be images but for right now I am just trying to get text files to transfer. Any help/code/ideas would be a lot of help. This is going to be triggered a button on a page by the way. I've tried this code and a few others, but it only displays 0. I'd like the simplest working way to display the number of files in a directory. Thanks for the help! $directory = "../images/team/harry/"; $filecount = count(glob("" . $directory . "*.*")); echo $filecount; Okay, so I'm kind of a PHP noob.
But out of context, for this site that I'm designing, it's easiest if I make a directory in the temporary folder PHP uses. In my case, /tmp/ because I am on Linux.
I want to use rand() to generate a random name for the page. But I then realized something, rand() could produce duplicates. How do I prevent PHP from trying to make the same directory at the same time? I know there are functions that will check if a file exists but I'm assuming it'll fail if that directory is currently being created, right?
How do I assure thread safety?
I willing to change my idea and not use rand(). Is there a way to get a unique key for each anonymous user on my site?
Hey guys im automatically generating a folder location to place some image files the user has uploaded into. If this folder doesnt exist how can i make it? $target_path_thumbnails = "http://www.worldwidelighthouses.com/Images/".$folderlocation."/".$foldername."/Mini/"; Thats the path style. Danny Hi everyone, I'm still learning, but getting intermediate in PHP now, but it is still challenge to learn. I'm trying to have php check to see if one file inside folder in server, seem I could not get it right, but I tested it on other site, it works, but not this script, I don't understand why it won't work...maybe logical is wrong? here my code: if ($_POST['video']) { $path1 = "UPLOADS/Home/"; $path2 = "UPLOADS/Breakfast/"; $path3 = "UPLOADS/Spider/"; $scan1 = scandir($path1); $scan2 = scandir($path2); $scan3 = scandir($path3); $count1 = count($scan1) - 3; $count2 = count($scan2) - 3; $count3 = count($scan3) - 3; if($count1 > 0) { header('location:exist.html'); }elseif ($count2 > 0) { header('location:exist.html'); }elseif ($count3 > 0) { header('location:exist.html'); } But other site, it works: $scan = scandir($path); $count = count($scan) - 3; echo $count; if($count > 1){ echo "Hello yourself!<br />"; } Anyone will help will be appreciate! Thank you! Gary Edited April 3, 2019 by sigmahokiesHello all, I am working on an upload script for a client. The script generates a random 32 bit string (to use as an ID). I want it to check if that folder exists and, if not, create it and put the files in it. Here is the script I have so far: <?php /******************************************* /* contact_us_process.php /* Author: Brandon Pence (brandonpence@gmail.com) /* /* Desc: This page process and checks the submitted form. /* /* variable meaning /* -------- ------- /* $FieldCheck Instance of the FieldCheck class. /* /******************************************/ session_start(); require ("./inc/include.inc.php"); //include Field Check Class include("classes/FieldCheck.php"); $FieldCheck = new FieldCheck; if(file_exists("uploads/$id")){ echo 'Folder Exists!'; }else{ echo 'Folder does not exist. Create folder.'; } //mkdir("uploads/$id",0777,true); $target_path = "uploads/$id/"; $i = 0; while($i < 3){ $target_path = $target_path . basename( $_FILES['uploadedfile']['name'][$i]); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$i], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name'][$i]). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } $i++; } if (!$FieldCheck->checkName($your_name)){ $errors['your_name'] = "Your name is not valid.";} if (!$FieldCheck->isEmpty($your_date)){ $errors['your_date'] = "Your date is not valid.";} if (!$FieldCheck->isEmpty($your_message)){ $errors['your_message'] = "Your message is not valid.";} foreach($_POST as $key=>$value){ $fields[$key] = $value; } //print_r($_POST); //check for error messages if (isset($errors)) { $_SESSION['errors'] = $errors; $_SESSION['fields'] = $fields; $_SESSION['error_msg'] = "Errors found! Please review your entries."; $url = "./testimonials.php?action=errors"; header ("Location: $url"); exit; }else{ // multiple recipients $to = 'email@email.com'; // note the comma // subject $subject = '[Domain Name.com] Testimonials Form Submission'; // message $message = ' Name: '.$your_name.' Date: '.$your_date.' Message: '.$your_message.' '; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'To: Example <email@example.com>' . "\r\n"; $headers .= 'From: Example <email@example.com.com>' . "\r\n"; $headers .= 'X-MAILER: PHP'.phpversion(); $headers .= 'Reply-To:email@example.com'; // Mail it $mail = mail($to, $subject, $message); //mail("$to", "$subject", "this is a test message, if you are reading this the email was sent successfully."); //savvy $_SESSION['error_msg'] = "Your email has been sent. Thank you for contacting us."; header( "Location: testimonials.php?action=success" ); } ?> It always outputs folder exists, even though it doesn't. My form is this: function random_string($p_length) { $char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $char_list .= "abcdefghijklmnopqrstuvwxyz"; $char_list .= "1234567890"; $random = ""; srand((double) microtime() * 1000000); for($i = 0; $i < $p_length; $i++) { $random .= substr($char_list, (rand() % (strlen($char_list))), 1); } return $random; } <form method="POST" action="contact_us_process.php" class="cmxform" enctype="multipart/form-data"> <?php if ($_GET['action'] == 'success' && isset($_SESSION['error_msg'])){ echo '<div class="success">'.$_SESSION['error_msg'].'</div>'; unset($_SESSION['error_msg']); }else{ if(isset($_SESSION['error_msg'])){ echo '<div class="error">'.$_SESSION['error_msg'].'</div>'; unset($_SESSION['error_msg']); } } ?> <fieldset style="margin:20px;width:350px;height:450px;background-color:none;"> <legend>Contact Us</legend> <ol> <li> <label for="your_name">Your Name</label> <input type="text" name="your_name" size="50" class="textbox" <?php if (isset($errors['your_name'])){echo 'class="input_red"';}?> <?php if (isset($fields['your_name'])){echo 'value="'.$fields['your_name'].'"';}?>/> <?php if (isset($errors['your_name'])){ echo '<span class="error_box"> '.$errors['your_name'].'<span class="error-pointer"> </span></span>';}else{?> <span class="hint">Tell us your name! It's nice to meet you!<span class="hint-pointer"> </span></span> <?php } ?> </li> <div class="clear"></div> <li> <label for="your_date">Your Date</label> <input type="text" name="your_date" size="50" class="textbox" <?php if (isset($errors['your_date'])){echo 'class="input_red"';}?> <?php if (isset($fields['your_date'])){echo 'value="'.$fields['your_date'].'"';}?>/> <?php if (isset($errors['your_date'])){ echo '<span class="error_box"> '.$errors['your_date'].'<span class="error-pointer"> </span></span>';}else{?> <span class="hint">Let's keep in touch!<span class="hint-pointer"> </span></span> <?php } ?> </li> <div class="clear"></div> <li> <label for="your_message">Your Message</label> <textarea rows="3" cols="40" name="your_message" class="textarea"<?php if (isset($errors['your_message'])){echo 'class="input_red"';}?>><?php if (isset($fields['your_message'])){echo $fields['your_message'].'</textarea>';}else{echo '</textarea>';}?> <?php if (isset($errors['your_message'])){ echo '<span class="error_box"> '.$errors['your_message'].'<span class="error-pointer"> </span></span>';}else{?> <span class="hint">What's on your mind?<span class="hint-pointer"> </span></span> <?php } ?> </li> <li> <input name="uploadedfile[]" type="file" /> </li> <li> <input name="uploadedfile[]" type="file" /> </li> <li> <input name="uploadedfile[]" type="file" /> </li> <?php $random_string = random_string(32); //This will return a random 10 character string?> <input type="hidden" value="<?php echo $random_string;?>" name="id"/> <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> <div class="submit_box"> <input type="submit" value="Get in touch with us!" name="submit" class="submit"> </div> </ol> </fieldset> </form> Can anyone help me with the "if it exists, move on, if it doesnt, create folder" part? I have a file system browser i'm writing for a client. I would like to know if there's a way to determine if an object is a directory or file, so I can display a proper icon next to it depending on what it is. Any help is appreciated. Hello, Its my first day coding so please forgive me for some of the silly errors I might make.
I am writing a small upload script and I want to have some sort of orginazation by uploading images to a direcotry based on the day. The following is my code but it will fail to save the file Warning: file_put_contents(images/14-09-26/5425905a18bba.png): failed to open stream: No such file or directory This is becuase the directory with the data part is not created how should I go about first checking that this directory is there and then creating it ? <?php ini_set('display_errors',1); error_reporting(E_ALL); $today = date('y-m-j'); define('UPLOAD_DIR', 'images/' .$today. '/'); $img = $_POST['data']; $img = str_replace('data:image/png;base64,', '', $img); $img = str_replace(' ', '+', $img); $data = base64_decode($img); $file = UPLOAD_DIR . uniqid() . '.png'; $success = file_put_contents($file, $data); print $success ? $file : 'Unable to save the file.'; ?> [/b]Hi people I'm having problem with inplementing a check box delete form in the jquery directory tree ....Realy I need help with this ! I am using the php code below.... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>jQuery Drop Down Menu</title> <!-- CSS For The Menu --> <link rel="stylesheet" href="stylee.css" /> </head> <body> <!-- Menu Start --> <div id="jQ-menu"> <?php error_reporting(0); $path = "store/".$diro."/"; function createDir($path = '.') { if ($handle = opendir($path)) { echo "<ul>"; while (false !== ($file = readdir($handle))) { if (is_dir($path.$file) && $file != '.' && $file !='..') printSubDir($file, $path, $queue); else if ($file != '.' && $file !='..') $queue[] = $file; } printQueue($queue, $path); echo "</ul>"; } } function printQueue($queue, $path) { foreach ($queue as $file) { printFile($file, $path); } } echo"<form action='osnovni_dokumenti.php?id=" . $data['ideo']. "' method='POST'>"; function printFile($file, $path) { echo "<li><input type='checkbox' name='del[]' value='$file'/><a href=\"".$path.$file."\"><img src='slike_izgled/file.png'> $file</a></li>"; } function printSubDir($dir, $path) { echo "<li><img src='slike_izgled/directory.png' width='20' height='20'> <span class=\"toggle\">$dir</span>"; createDir($path.$dir."/"); echo "</li>"; } createDir($path); echo"<p align='center'><input type='submit' name='filedel' value='Delete'></p> </form>"; ?> </div> <!-- End Menu --> <!-- Add jQuery From the Google AJAX Libraries --> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script> <!-- jQuery Color Plugin --> <script type="text/javascript" src="jquery.color.js"></script> <!-- Import The jQuery Script --> <script type="text/javascript" src="jMenu.js"></script> </body> </html> if(isset($_POST['filedel'])) { $id = $_POST[del]; $count = count($id); for ($i=0; $i<$count; $i++){ $files = $_POST['del']; echo"$files.$count"; } } Hi friends,
I have to transform this url domain.com/main/folder1/folder2/folder3 to
domain.com/get.php?var1=folder1&var2=folder2&var3=folder3
How can i do it with htaccess.
Thank u
Edited by exarhis, 08 July 2014 - 04:42 AM. I am attempting to us glob to display contents of a users folder using a session variable. Example: I have a session variable called department Code: [Select] $row_fullname['department']; In department I have the name of the department the user belongs to such as: office, plant, maintenance, and groundskeeping I created a folder called docs inside of docs there are 4 subfolders called office, plant, maintenance, and groundskeeping I found this code which will display the contents of the folder: Code: [Select] <?php $files = glob( './docs/office/*.*' ); foreach ( $files as $file ) { echo '<a href="./docs/office/' . basename( $file ) . '"target="_blank">' . basename( $file ) . '</a><br />'; } ?> The above code works fine, but I would like it to only display the contents of a departments folder only if the user is part on that department. Here is an example that I know is completely wrong but it may help explain what I am trying to do. Code: [Select] <?php ]<?php $files = glob( './docs/echo $row_fullname['department'];/*.*' ); foreach ( $files as $file ) { echo '<a href="./docs/echo $row_fullname['department'];/' . basename( $file ) . '"target="_blank">' . basename( $file ) . '</a><br />'; } ?>Thanks for your time I want an if statement that not only checks for the existence of a variable, but which also checks all the variables already created for one with a specific value. For example, I have a loop which creates variables, but I don't want it to make two variables with the same value. The problem is, using
if(isset(${'h'.$x}==false) && ${'h'.$x} != 3{ }won't work since the variable equal to 3 could be named h4, whereas this one will be named h5. I hoped I explained this efficiently enough, sorry about any confusion. Hi , I have a login page that i acces via www.mydomain.com/logib.php?last_visited_page.php however the passed in variable last_visited_page.com is not always set . once the login script has run , when the variable it is not set i would want jump to myaccount.php otherwise jump to last_visited_page.php How do i check for no variable beinf present and therefore jump to myaccount.php Many Thanks Fraser i am pulling the login date from the databases and i want it to check if the variable is empty and if it is echo Never Loged In and if it not empty echo the date <?php include'db.php'; $id=$_GET['id']; $sql=mysql_query("SELECT * FROM admin WHERE id='$id'")or die(mysql_error()); while($row=mysql_fetch_array($sql)) { $date=$row['login_date']; if($date > 0) { echo"Never Loged In"; } else { echo"$date"; } } ?> I was wondering if it is possible to check the content for a variable for a certain work. For example say a variable called "$variable1" was equal to "one, two, three, four, five". How can I check to see if the variable contain the word "four"? Thanks for any help. Hello, how do you search for a filename containing a date and then match it to a variable? Please note the filename may only contain some of the variable being matched to. Any help appreciated... I got this script: But it give me error, file_get_contents cannot open stream. I need to add the FTP connection with user/pass paramaters. then look in set http url, to get the file contents(images) and transfer to ftp server location. Can Anyone take alook and tell me if I am going down the right path and how to get there. Please Code: [Select] function postToHost($host, $port, $path, $postdata = array(), $filedata = array()) { $data = ""; $boundary = "---------------------".substr(md5(rand(0,32000)),0,10); $fp = fsockopen($host, $port); fputs($fp, "POST $path HTTP/1.0\n"); fputs($fp, "Host: $host\n"); fputs($fp, "Content-type: multipart/form-data; boundary=".$boundary."\n"); // Ab dieser Stelle sammeln wir erstmal alle Daten in einem String // Sammeln der POST Daten foreach($postdata as $key => $val){ $data .= "--$boundary\n"; $data .= "Content-Disposition: form-data; name=\"".$key."\"\n\n".$val."\n"; } // Sammeln der FILE Daten if($filedata) { $data .= "--$boundary\n"; $data .= "Content-Disposition: form-data; name=\"".$filedata['name']."\"; filename=\"".$filedata['name']."\"\n"; $data .= "Content-Type: ".$filedata['type']."\n"; $data .= "Content-Transfer-Encoding: binary\n\n"; $data .= $filedata['data']."\n"; $data .= "--$boundary--\n"; } // Senden aller Informationen fputs($fp, "Content-length: ".strlen($data)."\n\n"); fputs($fp, $data); // Auslesen der Antwort while(!feof($fp)) { $res .= fread($fp, 1); } fclose($fp); return $res; } $postdata = array('var1'=>'today', 'var2'=>'yesterday'); $filedata = array( 'type' => 'image/png', 'data' => file_get_contents('http://xxx/tdr-images/images/mapping/dynamic/deals/spot_map') ); echo '<pre>'.postToHost ("localhost", 80, "/test3.php", $postdata, $filedata).'</pre>'; $showCountSql = "select cad_count from counteraccountdtl WHERE cad_userid =".$_SESSION['UID']." LIMIT 1"; $showCountresult = mysql_query($showCountSql); $showCountrow = mysql_fetch_array($showCountresult); $newCount = $showCountrow[cad_count]; if(is_int($newCount)) echo "Value is Integer"; else echo "Value not Integer"; i m fetching value by Mysql "cad_count integer(5)" and feild then i cheak this value is interger or not it show the "Value not Integer". What is wrong in it ??? |