Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable gets concatinated

Status
Not open for further replies.

sanjdhiman

Programmer
Jan 15, 2003
189
GB
I have a quick problem

when uploading a file im trying to use the userfile_name attribute for the name of the file that im uploading to be idsplayed like a previw of the file

so im putting in the img tag the following

<img src=php/agents/$userfile_name height=200 width=200>
ok so its taking the director yfine BUT

if the filename has spaces it only takes the first bit of the file name
e.g. water lillies.jpg
becomes water.jpg

is there away round this do i do something with the vriable userfile_name

thanks in advance

sanj
 
hi there i tried the print_r($_FILES) and set it to a test variable it showed the file to be a 1

ok im getting the file $userfile_name from a previous page in a form (which is set to a file from)

the variable is called $userfile
and i have used $userfile_name and that gets the name of the file from the variable

Does that help?
 
Ok the first chunk of code is the upload form

this is called upload.php
This is where i load the form up and ask the user to choose the file to be uploaded

<?
session_start();
include 'header.inc';
include 'form_func.php';
?>
<title>BAM - Property Service - Upload Image</title>
<table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;800&quot; border=&quot;0&quot;>
<tr>
<td bgcolor=&quot;#6273BE&quot;>&nbsp;</td>
<td bgcolor=&quot;#EEEEEE&quot;>&nbsp;</td>
</tr>
<tr>
<td width=&quot;15%&quot; align=&quot;center&quot; valign=&quot;top&quot; bgcolor=&quot;#6273BE&quot;>
<table width=&quot;100%&quot; cellpadding=&quot;4&quot; cellspacing=&quot;4&quot; border=&quot;0&quot;>
<tr>
<td align=&quot;center&quot; class=&quot;leftHeader&quot;>
upload image
</td>
</tr>
<tr>
<td align=&quot;right&quot; class=&quot;leftBody&quot;>
back to agents page
</td>
</tr>
<tr>
<td align=&quot;center&quot; class=&quot;leftBody&quot;>
<br><br><br><br><br><br><br><br><br><br><br><br>
</td>
</tr>
<tr>
<td align=&quot;center&quot; class=&quot;leftBody&quot;>
<img src=&quot;Images/BAMBottom.gif&quot; border=&quot;0&quot; height=&quot;86&quot; width=&quot;113&quot;>
</td>
</tr>
</table>
</td>
<td width=&quot;85%&quot; align=&quot;center&quot; valign=&quot;top&quot; bgcolor=&quot;#EEEEEE&quot;>
<table width=&quot;100%&quot; cellpadding=&quot;4&quot; cellspacing=&quot;4&quot;>
<tr>
<td colspan=&quot;2&quot; class=&quot;Title&quot;>Agent - Upload Company Logo</td>
</tr>
<? check_valid_agent(); ?>
<tr>
<td colspan=&quot;2&quot; class=&quot;Body&quot;>Upload your companies logo onto BAMs database, so whenever a user
searches for a property your logo will be placed next to each of your
properties</td>
</tr>
<tr>
<td colspan=&quot;2&quot;>&nbsp;</td>
</tr>
<tr>
<td class=body>Current Company logo is:</td>
<? echo &quot;</tr>&quot; . getagentlogo($valid_agent) .&quot;</tr>&quot;; ?>
</tr>
<form enctype=&quot;multipart/form-data&quot; action=&quot;uploadimage.php&quot; method=&quot;post&quot;>
<tr>
<td width=&quot;25%&quot; align=&quot;right&quot; class=&quot;Form&quot;>Upload New Image:</td>
<td width=&quot;75%&quot; align=&quot;left&quot;><input name=&quot;userfile&quot; type=&quot;file&quot;></td>
</tr>
<tr>
<td width=&quot;25%&quot; align=&quot;left&quot;><input type=&quot;hidden&quot; name=&quot;MAX_FILE_SIZE&quot; value=&quot;10000000&quot;></td>
<td width=&quot;75%&quot; align=&quot;left&quot;><input type=&quot;submit&quot; value=&quot;Send File&quot;></td>
</form>
</tr>

</table>
</td>
</tr>
</table>

<?
include 'footer.inc';
?>

This next bit of code is the confirmation / preview of the file that has been uploaded. The problem lies when previewing the img in the <img ....> tag because it concatinates the filename

I hope this helps.. thanks once again for ur help



<?
session_start();
include 'header.inc';
include 'form_func.php'
?>
<title>BAM - Property Service - Uploading Image.....</title>
<table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;800&quot; border=&quot;0&quot;>
<tr>
<td bgcolor=&quot;#6273BE&quot;>&nbsp;</td>
<td bgcolor=&quot;#EEEEEE&quot;>&nbsp;</td>
</tr>
<tr>
<td width=&quot;15%&quot; align=&quot;center&quot; valign=&quot;top&quot; bgcolor=&quot;#6273BE&quot;>
<table width=&quot;100%&quot; cellpadding=&quot;4&quot; cellspacing=&quot;4&quot; border=&quot;0&quot;>
<tr>
<td align=&quot;center&quot; class=&quot;leftHeader&quot;>
uploading image
</td>
</tr>
<tr>
<td align=&quot;right&quot; class=&quot;leftBody&quot;>
<a href=&quot;agent.php&quot; class=&quot;leftbody&quot;>back to agents page</a>
</td>
</tr>
<tr>
<td align=&quot;right&quot; class=&quot;leftBody&quot;>
<a href=&quot;upload.php&quot; class=&quot;leftbody&quot;>upload image</a>
</td>
</tr>
<tr>
<td align=&quot;center&quot; class=&quot;leftBody&quot;>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</td>
</tr>
<tr>
<td align=&quot;center&quot; class=&quot;leftBody&quot;>
<img src=&quot;Images/BAMBottom.gif&quot; border=&quot;0&quot; height=&quot;86&quot; width=&quot;113&quot;>
</td>
</tr>
</table>
</td>
<td width=&quot;85%&quot; align=&quot;center&quot; valign=&quot;top&quot; bgcolor=&quot;#EEEEEE&quot;>
<table width=&quot;100%&quot; cellpadding=&quot;4&quot; cellspacing=&quot;4&quot;>
<tr>
<td colspan=&quot;2&quot; class=&quot;Title&quot;>Uploading ....</td>
</tr>
<? check_valid_agent(); ?>
<tr>
<td colspan=&quot;2&quot; class=&quot;subtitle&quot;><strong>Uploading file ....</strong></td>
</tr>
<!--<tr>
<td colspan=&quot;2&quot;>&nbsp;</td>
</tr> -->
<tr>
<?
if ($userfile==&quot;none&quot;)
{
echo &quot;<td class=body>Problem: no file uploaded</td>&quot;;
exit;
}

if ($userfile_size==0)
{
echo &quot;<td class=body>Problem: uploaded file is zero length</td>&quot;;
exit;
}

$upfile = &quot;$DOCUMENT_ROOT/php/agents/$valid_agent/&quot; .$userfile_name;

if (!copy($userfile, $upfile))
{
echo &quot;<td class=body>Problem: Could not move file into directory</td>&quot;;
exit;
}

echo &quot;<td class=body>File uploaded successfully</td>&quot;;
$fp = fopen($upfile, &quot;r&quot;);
$contents = fread ($fp, filesize ($upfile));
fclose ($fp);

$contents = strip_tags($contents);
$fp = fopen($upfile, &quot;r&quot;);
fwrite($fp, $contents);
fclose($fp);
//$filecontents = fread($fp, filesize($userfile));
echo &quot;<tr><td class=form>Preview of uploaded file contents:</td></tr>&quot;;
$image = $userfile_name;
echo &quot;<tr><td colspan=2><img src=php/agents/$valid_agent/$userfile_name height=200 width=200> </td></tr>&quot;;
//add this to agents database
$url = &quot;$DOCUMENT_ROOT/php/agents/$valid_agent/$image&quot;;
//echo $url;
$conn=db_connect();

$result = mysql_query(&quot;UPDATE estateagent SET EstateLogo=\&quot;$url\&quot; WHERE Email=\&quot;$valid_agent\&quot;&quot;);
?>
</tr>
</table>
</td>
</tr>
</table>

<?
include 'footer.inc';
?>

THANKS AGAIN FOR UR HELP IT IS MUCH APPRECIATED

SANJ
 
when i used $_FILES['userfile']['name'] as you directed i did it in the following way

$test = $_FILES['userfile']['name'];

then within the <img tag i put

<img src=php/agents/$valid_agent/$test .....>

I chose a file with a space in its name e.g. water lil.gif
on the preview image was not uploaded
it returned in the URL on inspection, the following

and that is it

before i used the $_FILES option i got the following url


with the lil bit chopped off


Does that help u

thanks for finding the time much appreciated

thanks

sanj
 
Just a suggestion:

I typically refer to the file as $_FILES['userfile']['tmp_name'], as that is the way it is on the upload. I use this to test the file to make sure it is there and if it is not a zero length, then I copy the file to a new location.

If there is a space in the name i typically include a preg_relace call to take the spaces out and turn them into &quot;_&quot;.

$filename=preg_replace(&quot; &quot;,&quot;_&quot;,$_FILES['userfile']['name']);

or

$file = $_FILES['userfile']['name'];
$filename=preg_replace(&quot; &quot;,&quot;_&quot;,$file);


something like that anyway.

hope that helps Reid Givens
Webdeveloper
 
I can't say I read through all of that... but from your original post........

if a form variable is getting truncated at a space, that is almost always a result of you not enclosing it in quotes.

<input type=&quot;text&quot; name=&quot;first name&quot;>
is the variable first name

<input type=text name=first name>
is the variable first

Looks like they're finding you a better way to do the whole process, but that might just be a tidbit you want to file away for future knowledge.

(The quotes are the html standard anyway)

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top