New to PHP simple code, but cant get it to work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Grahamhart
Forum Commoner
Posts: 27
Joined: Wed Jun 11, 2008 6:05 am

New to PHP simple code, but cant get it to work

Post by Grahamhart »

Hi, im new to php and have made a simple code to generate HTML files using a form and a php script. My problem is that the script goes through without any errors but when I check the directory the file hasnt been generated. I am sure it has permission to write as I had it generating files, problems came when I put in the html code to write, any help would be much appreciated, thanks.

My code is as follows.

<?php

/* If file_put_contents doesn't exists, let's make our own */
if( !function_exists('file_put_contents') ) {
function file_put_contents($file_name, $file_contents) {
/* Open the file */
if( !$handle = fopen($file_name, 'w') ) {
trigger_error("Cannot open file ($file_name)", E_USER_ERROR);
return false;
}
/* Write to it */
if( fwrite($handle, $file_contents) === FALSE ) {
trigger_error("Cannot write to file ($file_name)", E_USER_ERROR);
return false;
}
/* Close it */
fclose($handle);
return true;
}
}

$filename = $_POST["filename"];
$title = $_POST["title"];
$location = $_POST["location"];
$salary = $_POST["salary"];
$company = $_POST["company"];
$role = $_POST["role"];
$person = $_POST["person"];
$ref = $_POST["ref"];
$consultant = $_POST["consultant"];

$html = "

<html>
<head>
<title>$title</Title>
</Head>

<body>

<table border='1' bordercolor='#747474' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td>
<Table width='700' border='0' style='font-family:arial;font-size:80%;'><form enctype='multipart/form-data' method='post' action='application.php'>
<tr>
<td width='600' align='center'>
<img src='../images/spechead.gif'>
</td>
<td width='*' valign='top' align='right' rowspan='5'>
<img src='../images/rightcorner.gif' width='100'>
</td>
</tr>
<tr>
<td align='center' style='font-family:arial;font-size:200%;'>
<b>$title</b>
</td>
</tr>
<tr>
<td align='center' valign='top'>
$location
</td>
</tr>
<tr>
<td align='center' valign='top'>
$salary
</td>
</tr>
</table>
<table width='700' border='0' style='font-family:arial;font-size:80%;'>
<tr>
<td colspan='2'>
<b>The Company - Our Client:</b><br>
$company
<Br><br>
<b>The Role - Main Duties and Responsibilities:</b><br>
$role
<br><br>
<b>The Person - Skills, Knowledge, Qualifications and Experience</b><br>
$person





<br><br>
<b>Company Fusion:</b><br>
Building on 20 years market experience, Company Fusion specializes in providing flexible solutions through permanent and contract assignments, managed services and a variety of integrated, service innovations. Our mission is to be the leading UK Recruitment ‘Firm’ of choice for employees, jobseekers and employers. As a result we are committed to delivering the highest quality recruitment services in the marketplace.
<br><br>
<b>Personal Representation – Apply Today ”if you don’t ask, you don’t get”:</b><br>
If you would like to discuss this or any of our vacancies in confidence please email a copy of your CV with contact details to: <a href='mailto:recruitment@comapnyfusion.com'>recruitment@companyfusion.com</a> and/or call 0845 270 1266 for an informal discussion.
<br><br>
<b>Referrals Scheme £100 - £250:</b><br>
Do you know someone who is suitable for this role? Do you know of a company recruiting? E-mail a copy of their CV with the reference number or company contact numbers/email and your details clearly stating <b>REFERRAL</b> in the subject to: <a href='mailto:recruitment@companyfusion.com'>recruitment@companyfusion.com</a> and you could receive between <b>£100 - £250</b> Gift Voucher of your choice!
</td>
</tr>
<tr>
<td colspan='2'>
&nbsp;
</td>
</tr>
<tr>
<td colspan='2'>
<b>To apply for this position please complete the form bellow:</b>
</td>
</tr>
<tr>
<td>Name:</td>
<td><input type='text' name='cand_name'></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type='text' name='cand_phone'></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input type='text' name='cand_email'></td>
</tr>
<tr>
<td>Your CV:</td>
<td><input type='file' name='filename' />&nbsp;&nbsp;<a href='../cvbuild.html' target='new'>Build a CV</a></td>
</tr>
<tr>
<td colspan='2'>&nbsp;</td>
</tr>
<tr>
<td colspan='2'>Help us to help you and tell us why you would be good for this position.</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><textarea cols='50' rows='10' name='comments'></textarea></td>
</tr>
<tr>
<td colspan='2'>
&nbsp;
</td>
</tr>
<tr>
<td colspan='2' align='center'><input type='submit' value='Apply For Role'><INPUT type='hidden' name='recipient' VALUE='recruitment@companyfusion.com, $consultant'><INPUT type='hidden' name='refrence' VALUE='$ref'></td>
</tr>
</table></form>
</td>
</tr>
</table>
</body>
</html>
";

file_put_contents('jobspecs/$filename', $html);
?>


Its my impression this should preety much instantly write the file into the specified folder. the html doc comes out at about 3kb
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: New to PHP simple code, but cant get it to work

Post by hansford »

firstly you need to wrap the html string in a heredoc. try this and then we'll go from there.

$html = <<<END
rest of your string
rest of your string
rest of your string
rest of your string
END;
Grahamhart
Forum Commoner
Posts: 27
Joined: Wed Jun 11, 2008 6:05 am

Re: New to PHP simple code, but cant get it to work

Post by Grahamhart »

I made this change and got the following error message

Parse error: syntax error, unexpected $end in /freeola/users/0/9/sr0354790/htdocs/workinprogress/specgen.php on line 147
Grahamhart
Forum Commoner
Posts: 27
Joined: Wed Jun 11, 2008 6:05 am

Re: New to PHP simple code, but cant get it to work

Post by Grahamhart »

moving end; to a new line stopped the error but i still am not generating a file.
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: New to PHP simple code, but cant get it to work

Post by hansford »

working on it. I never use file_put_contents - now I know why.
Grahamhart
Forum Commoner
Posts: 27
Joined: Wed Jun 11, 2008 6:05 am

Re: New to PHP simple code, but cant get it to work

Post by Grahamhart »

hehe, thanks. Always open to alternatives just seemed like a nice easy way out, just goes to show things are never what they seem :)
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: New to PHP simple code, but cant get it to work

Post by hansford »

this won't work b/c the whole thing is seen as a string
file_put_contents('jobspecs/$filename', $html);

this should work according to the manual:

$dir = "/jobspecs/";
$filename= $_POST['filename'];
$path = $_SERVER['DOCUMENT_ROOT'] . $dir . $filename;
file_put_contents($path, $html);
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: New to PHP simple code, but cant get it to work

Post by hansford »

maybe someone can tell us why this doesn't work as per manual.
In the meantime just use fopen() and be done with it.
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: New to PHP simple code, but cant get it to work

Post by hansford »

what gets me is I can get this to write a file if its in the same directory, but not if it's writing to another directory. AND it always returns TRUE even though the file wasn't written.
Grahamhart
Forum Commoner
Posts: 27
Joined: Wed Jun 11, 2008 6:05 am

Re: New to PHP simple code, but cant get it to work

Post by Grahamhart »

ok i tryed using this bit of code

$dir = "/jobspecs/";
$filename= $_POST['filename'];
$path = $_SERVER['DOCUMENT_ROOT'] . $dir . $filename;
file_put_contents($path, $html);


This gave me an error in the top bit of code line8 because it couldnt find the file to fopen() however removing that make the file_put_contents($path, $html); an undefined funcation.

I did look into the possibilities of using fopen() but this confused me somewhat. how do I open a file thats not yet been created?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: New to PHP simple code, but cant get it to work

Post by superdezign »

... Sounds like an issue of file permissions, to me.
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: New to PHP simple code, but cant get it to work

Post by nowaydown1 »

Well, as previously mentioned, your file_put_contents call won't work as you would expect with the way you have it now:

Code: Select all

 
file_put_contents('jobspecs/$filename', $html);
 
In PHP, double quotes have to be used if you want to do any sort of string interpolation (expansion). So, for that to work as you would expect you either need to do concatenation with single quotes, or switch to double quotes:

Code: Select all

 
file_put_contents("jobspecs/$filename", $html);
 
It's quite possible you might have some sort of disk permissions issue on top of the above. Outside of that, having $filename point directly to a $_POST variable is not safe. Make sure you clean that in some fashion. Hope that helps.
Grahamhart
Forum Commoner
Posts: 27
Joined: Wed Jun 11, 2008 6:05 am

Re: New to PHP simple code, but cant get it to work

Post by Grahamhart »

Thanks for all the help everyone, after alot of messing about i managed to get it working.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: New to PHP simple code, but cant get it to work

Post by superdezign »

Tell us what you did differently so that others may benefit from it.
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: New to PHP simple code, but cant get it to work

Post by hansford »

it's not a permissions problem. Possibly just a WS_FTP problem. It happens with fopen() as well. Sometimes-not all. The file doesn't exist, so fopen() should create it given the correct mode. You look into whatever directory you supposedly saved the file to and it's not there. Well, it is there, just hidden. if you do a if(file_exists($path)) then you know it's there.
Post Reply