PHP fwrite get assistance Please

I just need some guidence with this function.

I have a  FILE and in the same DIR is a folder named "pd".

I am attempting to use "GET" to assing the file name of the file to be written on and the file to grab the data from.  

I can get it to write to the corecct file.   BUT
It writes only the Name of the file that holds the data content?



After several attemps the file that is being writtin to looks like this.

BLAHBLAH
somecontentP643920pd/.txtP643920pd/.txtpd/P643920.txt

I need it to grab the contents of the data file and not the name so the file written to looks like so.

BLAHBLAH
Iamthe data
not the name




Thank you in advance.
I know it's so close.

Selvol


?php
 $Tt = $_GET["page"];
 $filename = ($Tt.".jpg");

  $Pd = "pd/";
  $Tf = file_get_contents("$Pd$Tt.txt");

$somecontent = ($Pd.$Tt.".txt");

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($somecontent) to file ($filename)";

    fclose($handle);

} else {
    echo "The file $filename is not writable";
}
?>

Open in new window

LVL 17
selvolAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Altin BardhiConnect With a Mentor Software EngineerCommented:
Hi Selvol,

I have eddited and tested the following code and it loks like it does what you need it to do.

Just need to create a txt file inside the pd/ folder called page.txt

Regards
<?php //Edited by Altin Bardhi - 09/04/2010 -> altinbardhi.com

//Asign meaningfull names to the variables in order to avoid confusion
$page_name = 'page.txt'; /* I have created a file inside the 'pd'
folder and written some content for testing purposes.
You can also create this file programatically if you need to. */
 
$folder_name = "pd/";
 
$page_name_contents = file_get_contents( $folder_name .''. $page_name );

$file_to_write = $folder_name . 'write_to_page.txt';  /* I skipped creating a file to write to as this file will be created anyway */

// Let's make sure the file exists and is writable first.
//(Make sure the path is correct first. Could also make use of the 'try' 'catch' blocks here)
if (is_writable($folder_name .''. $page_name))
{
			// In our example we're opening $filename in append mode.
			// The file pointer is at the bottom of the file hence
			// that's where $somecontent will go when we fwrite() it.
			if (!$handle = fopen($file_to_write, 'a')) {
				 echo "Cannot open file ($file_to_write)";
				 //exit;
			}
			else 
			   if ($handle = fopen($file_to_write, 'a'))
			   {//**************
		
					   // Write $page_name_contents to our opened file.
						if (fwrite($handle, $page_name_contents) == true) {
						echo "Success, wrote ($page_name_contents) to file ($file_to_write)";
						fclose($handle);
						}else if (fwrite($handle, $page_name_contents) == false) {
						echo "Cannot write to file ($file_to_write_to)";
						exit;
						}
			
					
			   }//*************Close else

} else {
       echo "The file $page_name is not writable";
       }
?>

Open in new window

0
 
selvolAuthor Commented:
Your script works fine.. And I'll use that  "meaningful name"  wisely.  
The problem I have is Getting the "$_GET" to be used as the names of the DATA and TARGET files.


They have the same names. and Are paired.     I just need to submit a FORM.  To this page and and have it use the get for the name of the files.


Basically what  have is 2 files That need to merge into each other.  They have the same names. The the TXT is in the PD folder and need to be written to the other file  EOF.


SORT OF LIKE THIS script I modified to work as I needed.   But it writes BOF.   


I have attached what I am working with...       At this point I do not want IPTC data.   I just want the data to be written to the bottom of the JPG.  SLoppy.
It's the start of a project like IPTC But not.  Sort of......  But  not.   I realize the BOF is the bottom.  But I have written the next page to parse from the bottom.


Selvol





$dynamic_page = $_GET["page"]

Open in new window

Renameto-Rar.txt
0
 
Altin BardhiConnect With a Mentor Software EngineerCommented:
0
 
Altin BardhiConnect With a Mentor Software EngineerCommented:
Hi Selvol,

Also,below I have attached some code that may be useful to you.

I tried to use as less code as possible for this solution.

Hope it helps.

Good luck with your project.
//Create an image dynamically
$im = imagecreatetruecolor(300, 300);
// $text_color = imagecolorallocate($im, 233, 14, 91); $text_color = imagecolorallocate($im, 0xff, 0xFF, 0xFF); //white
imagestring($im, 1, 0, 100,  'A Simple Text String', $text_color);

imagejpeg($im, './images/dynamic_image.jpg', 100); //Save the dynamic image


//Write a text to an existing image
$existing_image = imagecreatefromjpeg('./images/jo.jpg');
//$text_color = imagecolorallocate($existing_image, 233, 14, 91);
$text_color = imagecolorallocate($existing_image, 0xff, 0xFF, 0xFF); //white
// Load the custom gd font and write on the image
//$font = imageloadfont('./04b.gdf');
$font = imageloadfont('almosnow.gdf');  //http://www.freetowne.com/fonts/
imagestring($existing_image, $font, 140, 100,  'This is the text string, or we can place the text file content variable here.', $text_color);

// Output to browser
header('Content-type: image/jpeg');
imagejpeg($existing_image, './images/jo.jpg', 100); //Save the modified image
imagedestroy($existing_image);

Open in new window

0
 
selvolAuthor Commented:
Thank you for going above and beyond.


I appreciate the help and am a bit wiser now.

Selvol
0
All Courses

From novice to tech pro — start learning today.