PHP - Variables Between Php Files
Hi Guys,
I am relatively new to php, and have hit a problem. Basically I am writing a PHP socket server - which acts as a socket server for a swish movie. The only way of getting data into the swish movie is one function - loadvariablesnum() - which uses 'post' or 'get'. The swish movie is a web GUI for a system and therefore needs a reasonably decent refresh rate - 5 times a second for example. Originally I wrote a php file that created / bound / listened to the socket and then entered an infinite while loop that would read from the socket and echo the output to the swish movie. The problem with this method, was that each time the swish movie called loadvariablesnum() the PHP script would re-open - re-bind - re-listen to the socket - which caused all sorts of problems for my client. Therefore I decided to re-write the PHP file that included functions: Code: [Select] <? //TCP_Socket_Server.php global $output; function TCP_Socket_Server() { $host = "10.10.13.59"; $port = 10000; set_time_limit(0); $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); $result = socket_bind($socket, $host, $port);// or die("Could not bind to socket\n"); $result = socket_listen($socket, 3) or die("Could not set up socket listener\n"); socket_set_block($socket); $spawn = socket_accept($socket) or die("Could not accept incoming connection\n"); // read client input while(1) { $input = socket_read($spawn, 10000);// or die("Could not read input\n"); $input = trim($input); global $output; $output = $input; WriteData(); flush(); } socket_close($spawn); socket_close($socket); } function WriteData() { static $LocalTemp; global $output; $LocalTemp = $output; $TempBuffer = "&TCP_SocketServerBufferVar="; //$TempBuffer .= $GLOBALS['output']; $TempBuffer .= $LocalTemp; echo $TempBuffer; } ?> Using this method I can call TCP_Socket_Server() only once at the beginning of the swish movie using the loadvariablesnum() (from another PHP file), which sets the server running - e.g. Code: [Select] <? //Load_Server.php include 'TCP_Socket_Server.php'; TCP_Socket_Server(); ?> My plan was to then call the WriteData() function regularly approximately 5x per second, (again from another PHP file using loadvariablesnum() in swish): Code: [Select] <? //Write_Data.php include 'TCP_Socket_Server.php'; Write_Data(); ?> However, when I call this function from an external PHP file - no valid data is output by echo - even though I can see the data being received (in the browser) in the main Load_Server.php page - which is output using the same Write_Data() function. I have tried many different ways of accessing this variable ($output) from a different php file - e.g returning the variable from Write_Data() - which had the same result (i.e. no valid data). I have also tried using sessions, but could only seem to get them to work in files that did not run indefinitely - e.g. without the infinite while loop in TCP_Socket_Server(). I am currently running the PHP files (and the rest of the web content) on APACHE2 - in Ubuntu. I would really appreciate any advice on this - as at the moment, this looks like a show stopper - and would waste 2 months work. Similar TutorialsHi, I'm having a bit of a problem sharing some variables I have across a few files. My first file (index.php) calls an ajax request for (display.php) and sends a few variables to it. All fine so far. display.php then does its thing and displays the content on index.php as it should. Now in display.php I need a variable to be passed back to index.php for use in a menu. I've tried setting a session variable in display.php but unfortunately this doesn't seem to work. I also can't use get or post as I don't want to refresh the page. Really scratching my head over this one! Edd Hy! I have 3 files: file 1 (index.php) includes 2 files: file2.php and file3.php File2.php contains $aditionalStuff and in file3.php I want to use $aditionalStuff, but it wont work (like it wasn't initialized). How can I make this work? index.php include "file2.php"; include "file3.php"; file2.php $aditionalStuff = 'some stuff'; file3.php echo $aditionalStuff; Thanks! Hi, i'm new to php and am trying to call $variables in different in multiple hierarchy from one main variable file. I have one main file called variables.php that contain a list of variables <?php $variable1 = 'root/main/'; $variable2 = 'welcome'; $variable3 = 'testing'; ?> a second file called file1.php in the same directory as the variables.php file <?php require 'varibles.php'; include $variable1 . 'testing.php' ?> a third file called testing.php inside root/main/ <?php echo $variable2; include $varible1 . 'temp/abc.php'; ?> a fourth file called abc.php inside root/main/temp <?php echo $variable3; ?> the problem is testing.php is able to echo $variable2 but abc.php is not able to echo $variable3...is there a limit to the level of files before it can't echo a variable? This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=343041.0 So far I have managed to create an upload process which uploads a picture, updates the database on file location and then tries to upload the db a 2nd time to update the Thumbnails file location (i tried updating the thumbnails location in one go and for some reason this causes failure) But the main problem is that it doesn't upload some files Here is my upload.php <?php include 'dbconnect.php'; $statusMsg = ''; $Title = $conn -> real_escape_string($_POST['Title']) ; $BodyText = $conn -> real_escape_string($_POST['ThreadBody']) ; // File upload path $targetDir = "upload/"; $fileName = basename($_FILES["file"]["name"]); $targetFilePath = $targetDir . $fileName; $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION); $Thumbnail = "upload/Thumbnails/'$fileName'"; if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){ // Allow certain file formats $allowTypes = array('jpg','png','jpeg','gif','pdf', "webm", "mp4"); if(in_array($fileType, $allowTypes)){ // Upload file to server if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){ // Insert image file name into database $insert = $conn->query("INSERT into Threads (Title, ThreadBody, filename) VALUES ('$Title', '$BodyText', '$fileName')"); if($insert){ $statusMsg = "The file ".$fileName. " has been uploaded successfully."; $targetFilePathArg = escapeshellarg($targetFilePath); $output=null; $retval=null; //exec("convert $targetFilePathArg -resize 300x200 ./upload/Thumbnails/'$fileName'", $output, $retval); exec("convert $targetFilePathArg -resize 200x200 $Thumbnail", $output, $retval); echo "REturned with status $retval and output:\n" ; if ($retval == null) { echo "Retval is null\n" ; echo "Thumbnail equals $Thumbnail\n" ; } }else{ $statusMsg = "File upload failed, please try again."; } }else{ $statusMsg = "Sorry, there was an error uploading your file."; } }else{ $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, mp4, webm & PDF files are allowed to upload.'; } }else{ $statusMsg = 'Please select a file to upload.'; } //Update SQL db by setting the thumbnail column to equal $Thumbnail $update = $conn->query("update Threads set thumbnail = '$Thumbnail' where filename = '$fileName'"); if($update){ $statusMsg = "Updated the thumbnail to sql correctly."; echo $statusMsg ; } else { echo "\n Failed to update Thumbnail. Thumbnail equals $Thumbnail" ; } // Display status message echo $statusMsg; ?> And this does work on most files however it is not working on a 9.9mb png fileĀ which is named "test.png" I tested on another 3.3 mb gif file and that failed too? For some reason it returns the following Updated the thumbnail to sql correctly.Updated the thumbnail to sql correctly. Whereas on the files it works on it returns REturned with status 0 and output: Retval is null Thumbnail equals upload/Thumbnails/'rainbow-trh-stache.gif' Failed to update Thumbnail. Thumbnail equals upload/Thumbnails/'rainbow-trh-stache.gif'The file rainbow-trh-stache.gif has been uploaded successfully. Any idea on why this is? Hello I have a simple question about file handling... Is it possible to list all files in directories / subdirectories, and then read ALL files in those dirs, and put the content of their file into an array? Like this: array: [SomePath/test.php] = "All In this php file is being read by a new smart function!"; [SomePath/Weird/hello.txt = "Hello world. This is me and im just trying to get some help!";and so on, until no further files exists in that rootdir. All my attempts went totally crazy and none of them works... therefore i need to ask you for help. Do you have any ideas how to do this? If so, how can I be able to do it? Thanks in Advance, pros I am using WPSQT plugin in my blog site .I code some files in PHP also.how to add that files in plugin files.
does anyone know how to decode this XML variable value into string values? I also need to know the oposite way: creating variable values into xml. I've tried several code examples but they did filter the requested data. Code: [Select] $xml='<?xml version="1.0" encoding="utf-8"?> <elements> <text identifier="ed9cdd4c-ae8b-4ecb-bca7-e12a5153bc02"> <value/> </text> <textarea identifier="a77f06fc-1561-453c-a429-8dd05cdc29f5"> <value><![CDATA[<p style="text-align: justify;">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>]]></value> </textarea> <textarea identifier="1a85a7a6-2aba-4480-925b-6b97d311ee6c"> <value><![CDATA[<p style="text-align: justify;">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>]]></value> </textarea> <image identifier="ffcc1c50-8dbd-4115-b463-b43bdcd44a57"> <file><![CDATA[images/stories/red/cars/autobedrijf.png]]></file> <title/> <link/> <target/> <rel/> <lightbox_image/> <width><![CDATA[250]]></width> <height><![CDATA[187]]></height> </image> <text identifier="4339a108-f907-4661-9aab-d6f3f00e736e"> <value><![CDATA[Kramer 5]]></value> </text> <text identifier="ea0666d7-51e3-4e52-8617-25e3ad61f8b8"> <value><![CDATA[6000 RS]]></value> </text> <text identifier="90a18889-884b-4d53-a302-4e6e4595efa0"> <value><![CDATA[Eindhoven]]></value> </text> <text identifier="410d72e0-29b3-4a92-b7d7-f01e828b1586"> <value><![CDATA[APK Pick up and return]]></value> </text> <text identifier="45b86f23-e656-4a81-bb8f-84e5ea76f71f"> <value><![CDATA[15% korting op grote beurt]]></value> </text> <text identifier="3dbbe1c6-15d6-4375-9f2f-f0e7287e29f3"> <value><![CDATA[Gratis opslag zomerbanden]]></value> </text> <text identifier="2e878db0-605d-4d58-9806-8e75bced67a4"> <value><![CDATA[Gratis abonnement of grote beurt]]></value> </text> <text identifier="94e3e08f-e008-487b-9cbd-25d108a9705e"> <value/> </text> <text identifier="73e74b73-f509-4de7-91cf-e919d14bdb0b"> <value/> </text> <text identifier="b870164b-fe78-45b0-b840-8ebceb9b9cb6"> <value><![CDATA[040 123 45 67]]></value> </text> <text identifier="8a91aab2-7862-4a04-bd28-07f1ff4acce5"> <value/> </text> <email identifier="3f15b5e4-0dea-4114-a870-1106b85248de"> <value/> <text/> <subject/> <body/> </email> <link identifier="0b3d983e-b2fa-4728-afa0-a0b640fa34dc"> <value/> <text/> <target/> <custom_title/> <rel/> </link> <relateditems identifier="7056f1d2-5253-40b6-8efd-d289b10a8c69"/> <rating identifier="cf6dd846-5774-47aa-8ca7-c1623c06e130"> <votes><![CDATA[1]]></votes> <value><![CDATA[1.0000]]></value> </rating> <googlemaps identifier="160bd40a-3e0e-48de-b6cd-56cdcc9db892"> <location><![CDATA[50.895711,5.955427]]></location> </googlemaps> </elements>'; Hi. I have some code which needs to return a single variable from the function and stored as a variable within this page, so it can be echoed later on in the page. I couldn't get it to return as a variable, so i used "return compact();" to return the variable and "extract (myFunction());" to extract it to variables. However, when I turned on php's display errors and error reporting function, I got an error message saying "Warning: extract() [function.extract]: First argument should be an array in /my/web/site/index.php on line 6" (which is where my extract function is). This works fine with passing more than one variables through, is there another way pass one variable from a function to be stored as a variable on the page which called the function? Here is the function: Code: [Select] <?php //This is a list of numeric error codes against their text. this will return the error code as the variable $issue function checkLoginIssue() { //If there is an error code if (isset($_GET["issue"])) { //cycle through the list until the code is reached, return the text and break the switch switch ($_GET["issue"]) { case "1": $issue = '<p class="warning">Please log in to view this page</p>'; break; case "2": $issue = '<p class="warning">Wrong Username or Password</p>'; break; case "3": $issue = '<p class="warning">No user found with those details</p>'; break; } //return the variable in an array with a single value return compact('issue'); } } ?> And here is the code which calls the function: Code: [Select] <?php extract(checkLoginIssue()); ?> I have told that the php files I was working on contains BOM by default cause of the editor I m using. I have installed other editor that can create php files without BOM. So my question is what's the difference between 2 files, one with BOM and other without BOM ? Any problems I may face for long term usage ? Which one is better to use ? Hey, Trying to learn to generate PDF files, something apparently WAAAAYYY more complex than I thought. Can someone please give me an explanation on '$optlist' from this function..... PDF_load_image ( resource $pdfdoc , string $imagetype , string $filename , string $optlist ) What is it? PHP.net doesn't have any info at all. Thanks, IceKat. BTW - Any (recent) tutorials or pages would be appreciated. Everything I find is out of date or doesn't work. first of all...its good to see the site back .
my question guys is i'm wondering is there a way of getting the column number when reading a .cvs file?
ie. a, b, c, d, aa, ab etc?
$row = 1; if (($handle = fopen($file, "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; for ($c=0; $c < $num; $c++) { //echo $data[$c] . "<br />\n"; } } fclose($handle); }now $num counts the columns...but i want to convert that into a alphabetical column...is there a easy solution? thanks guys lets say I own a file, and you own a file, both these files are the exact same file, the only difference is the filename. So lets say we both have The Gimp 2.6 but one is named TG2.6.exe and the other is named the_Gimp-2.6.exe. What would be the best way to count the files that are the same even if they have a different file name? Another example would be: I have a file called me.jpg and someone else has a file me.jpg but they are two completely different pictures. What would I do to tell that? I have a section where users can upload documents into our software. The code itself is a couple years old and could perhaps use some updating, but it seems some of the files (at this time it only seems to be *.pdf) that have been uploaded are not able to be opened and the error "the file is damaged and could not be repaired" is being displayed as an "Adobe Reader" error in Internet Explorer 8. The largest concern is that it is an irreversible error and many of the documents users have uploaded are now corrupt. I can't seem to find any relevant information from Google (or Adobe) that does anything towards resolving the problem. Note: This does not happen on every .pdf, and I am not sure if there is a common bond between the failing documents (trying to find this now). hey, can some one provide a code to help me copy a folder called contents with files in it to another folder called items cheers Hello, I'd like to make an array preferably multidimensional from files what are in subdirectory. Here is a code what I wrote but I doesn't work well becouse I wanted to load that string into javascript array..but it didnt work becouse it had some special characters.. Code: [Select] $dir = opendir('f'); if($dir) { $result = ""; while (($file = readdir($dir)) !== false ) { if(strpbrk($file, 'mp3')) { $result = $result ."{ Track name: \"$file\", location: \"f/$file\" },<br/>"; } } echo $result; } Hello, I've been assigned the task of creating an internal site for our office that has links to all the websites we use as well as the applications we use. I'm very comfortable using php with mysql for handling forms and displaying database information but I'm not sure how to go about this. So my question is, is there anyway I can use php to open an exe file? I've searched around the internet and can't find a good method to do this. To start, I have a database with a field called 'fpath' that stores the filepath (root-relative) to uploaded PDF's. In this case, they're scanned personnel files. What I'd like to do is be able to download relevant PDF's based on a generated report... such as all PDF's of a certain category or all PDF's for a certain user. If there's a specific PEAR package or other script out there that you know works well, I can research it on my own.. but each search result is bringing up a different method to do it, which makes me nervous. Thanks! I know how to count the files: <?php $dir = $_SERVER[DOCUMENT_ROOT]."/api/sigs/cache/".$i[id]."/"; $files = scandir($dir); $result = count($files); printf("$result Signatures Generated"); ?> But i'm trying to make it so it only counts the files that were modified recently (That Day). |