Category Archives: php

Composer cannot download files

Questions: I am trying to use composer on command line : php composer.phar update php composer.phar install php composer.phar self-update php composer.phar selfupdate Whatever I do I always receive the same error message: File could not be downloaded. Failed to open stream I am behind a proxy. If I use a browser and type the… Read More »

Understanding nested PHP ternary operator

Questions: I dont understand how that output (“four“) comes? $a = 2; echo $a == 1 ? ‘one’ : $a == 2 ? ‘two’ : $a == 3 ? ‘three’ : $a == 5 ? ‘four’ : ‘other’ ; // prints ‘four’ I don’t understand why “four” gets printed. Answers: You need to bracket the… Read More »

How to insert multiple rows from a single query using eloquent/fluent

Questions: I have the following query: $query = UserSubject::where(‘user_id’, Auth::id())->select(‘subject_id’)->get(); and as expected I get the following result: [{“user_id”:8,”subject_id”:9},{“user_id”:8,”subject_id”:2}] Is there a way of copying the above result into another table so that my table looks like this? ID|user_id|subject_id 1 |8 |9 2 |8 |2 The problem I have is that the $query can expect… Read More »

Category: php

PHP get file listing including sub directories

Questions: I am trying to retrieve all images in a directory, including all subdirectories. I am currently using $images = glob(“{images/portfolio/*.jpg,images/portfolio/*/*.jpg,images/portfolio/*/*/*.jpg,images/portfolio/*/*/*/*.jpg}”,GLOB_BRACE); This works, however the results are: images/portfolio/1.jpg images/portfolio/2.jpg images/portfolio/subdirectory1/1.jpg images/portfolio/subdirectory1/2.jpg images/portfolio/subdirectory2/1.jpg images/portfolio/subdirectory2/2.jpg images/portfolio/subdirectory1/subdirectory1/1.jpg images/portfolio/subdirectory1/subdirectory1/2.jpg I want it to do a whole directory branch at a time so the results are: images/portfolio/1.jpg images/portfolio/2.jpg images/portfolio/subdirectory1/1.jpg images/portfolio/subdirectory1/2.jpg… Read More »

How to check that an object is empty in PHP?

Questions: How to find if an object is empty or not in PHP. Following is the code in which $obj is holding XML data. How can I check if it’s empty or not? My code: $obj = simplexml_load_file($url); Answers: You can cast to an array and then check if it is empty or not $arr… Read More »

PHP move_uploaded_file() error?

Questions: I using following code and it is successfully uploading files on my local machine. It is showing “Successfully uploaded” on my local machine. // Upload file $moved = move_uploaded_file($_FILES[“file”][“tmp_name”], “images/” . “myFile.txt” ); if( $moved ) { echo “Successfully uploaded”; } else { echo “Not uploaded”; } But when I used this code on… Read More »

Best PHP Encoder [closed]

Questions: How does the php encoder works? Do they need any server side software installation? How I use them? Which one from the below is best PHP encoder? ioncube sourceguardian phpshield phpcipher truebug Answers: most of the php encoder requires server side software installation. Please see the documentation of using them, as each have their… Read More »

retrieve data from db and display it in table in php .. see this code whats wrong with it?

Questions: $db = mysql_connect(“localhost”, “root”, “”); $er = mysql_select_db(“ram”); $query = “insert into names values(‘$name’,’$add1′,’$add2′,’$mail’)”; $result = mysql_query($query); print “<p> Person’s Information Inserted </p>”; $result = mysql_query(“SELECT * FROM names”); ?> <table border=”2″> <tr> <th>Name</th> <th>Address Line 1</th> <th>Address Line 2 </th> <th>E-mail Id </th> </tr> <? while ($array = mysql_fetch_row($result)); { print “<tr> <td>”;… Read More »

Handling plupload's chunked uploads on the server-side

Questions: When I use plupload to chunk files (setting option chunk_size), I get a separate PHP request for each chunk. Looking at $_FILES variable, each chunk is of type “application/octet-stream”. Is there any simple, standard and comfortable way how to combine these pieces in PHP at server-side? With sanity guaranteed (e.g. when one of the… Read More »

Category: php

Validating US phone number with php/regex

Questions: EDIT: I’ve mixed and modified two of the answers given below to form the full function which now does what I had wanted and then some… So I figured I’d post it here in case anyone else comes looking for this same thing. /* * Function to analyze string against many popular formatting styles… Read More »