Re: Variable post as array
| From: | Colin Guthrie | Date: | Wed, 05 Mar 2008 13:46:49 +0000 |
| Subject: | Re: Variable post as array | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-271012@lists.php.net to get a copy of this message | ||
Pieter du Toit wrote:
> That is the problem, i can see the name of the file with $txtPhoto['name']
> but when i use the same with temp_name, i get nothing, one weird thing is
> this [error] => 0 [size] => 17008 , what is that?
>
> this is the code that is giving the problem
> $txtPhotoData = addslashes(fread(fopen($txtPhoto, "r"),
> filesize($txtPhoto)));
>
> And this is the error
>
> Warning: fopen() expects parameter 1 to be string, array given in
>> /usr/www/users/zululr/marketplace/myzululand/specials_proc.php on line 49
>
> I did not do the code im just trying to help out, if never use this kind of
> code to work with photos, i normally use move_uploaded_file and put the path
> in the databse, but it look like this code puts the photo in the database as
> binary or something.
OK, I'll try and spell it out seeing as the link to the PHP Manual
didn't take ;)
The variable $txtPhoto exists because you have register global option set.
It will be the same value as $_FILES['txtPhoto'].
This variable *is an array*. It is not meant to contain just a filename.
Your fopen() statement assumes that this variable *is a string* and this
is fundamentally wrong.
The PHP Manula page cannot be more clear about what the various elements
in this array mean:
http://uk.php.net/manual/en/features.file-upload.php
(here their element is called 'userfile', yours is called 'txtPhoto'.
$_FILES['userfile']['name']
The original name of the file on the client machine.
$_FILES['userfile']['type']
The mime type of the file, if the browser provided this information.
An example would be "image/gif". This mime type is however not checked
on the PHP side and therefore don't take its value for granted.
$_FILES['userfile']['size']
The size, in bytes, of the uploaded file.
$_FILES['userfile']['tmp_name']
The temporary filename of the file in which the uploaded file was
stored on the server.
$_FILES['userfile']['error']
The error code associated with this file upload. This element was
added in PHP 4.2.0
The ['name'] is the original name of the file the person uploaded. Your
code tries to load the contents of the file into a variable. To do this,
this simplest way is to do:
$photoData = file_get_contents($_FILES['txtPhoto']['tmp_name']);
Your code calls addslashes() on it but that's pretty braindead as it's
binary data. If it's going into a database you'd be better formatting it
using a database formatting function (e.g. mysql_real_escape_string) and
not just relying on an ad-hoc method.
HTHs
Col