Forum Moderators: coopster

Message Too Old, No Replies

uploading pictures

into a webhost for later display

         

Baruch Menachem

4:15 am on Sep 25, 2009 (gmt 0)

10+ Year Member



A friend of mine is doing a small business where he wants to add pictures of his product on a regular basis, have a central picture for his main page, and be able to have his products stored by type.

I have the following two lamo scripts I am trying to use for the picture upload, which is !working :P at all.

The form

<body>
<fieldset>
<form name ="form1" method="post" action="check_image.php" enctuype="multipart/form-data">

<table border="0" cellpadding="5">
<tr><td>This is the central display item </td><td><input name="display" type="checkbox" id="display"></td>

<tr>
<td> Item name</td><td> <input name="item_name" type="text" id="item_name" size="22" maxlegnth="22"> </td>
</tr>

<td> item type</td>

<td>
<select name="thistopic">
<?php
$view="select * from inventory order by type";
$result=mysql_query($view) or die("out of cheese error, redo from start".mysql_error());
while($row=mysql_fetch_array($result))
{
$topic=$row['type'];
$topic_id=$row['number'];
if ($topic_id==6)
$sel="Selected";
else $sel=" ";
$cat=<<<MEOW
<option value ="$topic_id" $sel>$topic</option>

MEOW;
echo $cat;

}
?>
</select>

</tr>
<tr><td> Set the price </td><td> <input name="tsena" type="text" id="price" size="10"></td</tr>
<tr>

<td>
upload image:
</td>
<td>
<input name="image_filename" type="file" id="image_filename" >
</td>
</tr>
</table>
<br />
<em> please use only Jpeg, Gif or PNG type files</em><br />
<p>
<input type="submit" name="submit" value="submit"> &nbsp <input type="reset" name="submit2" value="clear form">
</p>
</form>
</fieldset>

and the next page. The fail seems to take place at the "move_uploaded_files line."

I called the web host and they recommended the ~/public_html/ as the file path, unless I misunderstood their instruction.


$item_name=$_POST['item_name']; // name of the item
$type=$_POST['thistopic']; //what kind of thing is it
$price=$_POST['tsena'];
$image=$_POST['image_filename'];
$image_temp=$_FILES['image_filename']['name'];// This is copied from a textbook I am using. I don't know where name is coming from in the files array.
$today= date("Y-m-d");

$image_dir="~/public_html/";
$imageName=$image_dir.$image_temp;
if (move_uploaded_file($_FILES['image_filename']['tmp_name'],$imageName))
// I don't know where tmp_name is coming from either.

{
list ($width, $height, $type, $attr)=getimagesize($imageName);
switch ($type)
{
case 1:
$ext=".gif"; break;
case 2: $ext=".jpg"; break;
case 3: $ext=".png"; break;
default:
echo "Sorry, but the file you uploaded was not of the correct type. ";
echo "Please hit the browser back button and try, try again";

}

}
else echo "So sorry, failed for some reason";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>verify photo submission. Please wait</title>

</head>

rocknbil

10:21 pm on Sep 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right off the bat

enctuype

needs to be enctype. This is particularly important in uploads, as multipart-form/data is what separates the image attachments from regular encoded data, and with this typo it's probably posting as text/plain.

out of cheese error

LOL . . . I used to keep myself amused with such comments, but then realized someone some day may not appreciate my humor. :-)

I'll keep looking. :-)

Baruch Menachem

10:43 pm on Sep 25, 2009 (gmt 0)

10+ Year Member



Thanks. I fixed it and called the ISP back. It turns out the problem was the line

$image_dir="~/public_html/";

I needed to skip the slashes. Or there is another issue. does not use the directory path as a path, but as part of the file name. So when I get the thing to work Glory glory, I still have the issue of having "public_html" as part of my file names.

rocknbil

4:17 pm on Sep 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe I'm missing it . . . public_html is usually a system directory aliased as the domain root, so it's not actually in a web page URL. Example:

/var/www/example.com
/var/www/example.com/cgi-bin
/var/www/example.com/stats
/var/www/example.com/some-other-dir-inaccessible-to-web
/var/www/example.com/public_html

In the above, a request for "example.com/filename.php" will only find filename.php if it's in the public_html directory. So within those pages, you'd never have "public_html" in any links to images or other pages. In fact, you should get a 404 if you request "example.com/public_html/filename.php." You should also get a "permission denied" for any request outside "public_html" (except stats, cgi-bin, etc.)

Otherwise, you can look into mod_rewrite, you should be able to rewrite URL's so you don't need public_html in the URL.

Edit: in re-review, I think you're making a common error here. A system path and a relative URL are not the same thing, although many scripts are written in such a way that they appear to be the same thing.

When you write to a file (or move_uploaded_file) you use the system path:

$write_image_dir = '/var/www/example.com/public_html/images';

You could try to use the current directory you're in and make that relative, but the potential always exists some program or process changes directory on you causing it to fail.

When you write a link for the file that's been uploaded, the url is relative the the domain root, which I'm guessing is public_html. So from there, the link to the images directory would be

$public_image_dir = '/images';

The leading slash always means "start at domain root and look from here," which in your case would mean it's starting from public_html.

Here's a quick copy and paste of a working script doing a similar thing. In my case, the uploaded file is "photo". It may be confusing but it's better if you get a handle on the concepts by example, you'll be able to figure out what's up.

// These are defined as constants in a config file. Those are:
//define('ADS_DIR','/var/www/vhosts/example.com/httpdocs/images/uploaded');
//define('ADS_RELATIVE_DIR','images/uploaded');

$uploads_dir = ADS_DIR;
$ads_rel = ADS_RELATIVE_DIR;

// Note some extra variables here, echoing their values
// in comments <!-- --> will be helpful. Note that this
// should answer some questions you have about where
// the keys come from, they are generated by PHP

$name = $_FILES['photo']['name']; // Redundant, yes!
$type = $_FILES['photo']['type'];
$tmp_name = $_FILES['photo']['tmp_name'];
$size = $_FILES['photo']['size'];
$error = $_FILES['photo']['error'];

// This is in a thumbnail and large image function,
// hence the variable names - but note, two values,
// a system path and relative URL for links. $qs is
// my "cleansed" array from $_POST

$large_image = "$ads_rel/" . $qs['photo'];
$large_path = "$uploads_dir/" . $qs['photo'];

$test = move_uploaded_file($tmp_name,$large_path);

[edited by: rocknbil at 4:43 pm (utc) on Sep. 26, 2009]

Baruch Menachem

4:27 pm on Sep 26, 2009 (gmt 0)

10+ Year Member



So, if I understand this, what you are saying is I can create an images directory and then use /directoryname/filename

Since pulbic_html does not exist, it can't find it, right?

rocknbil

4:34 pm on Sep 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See edited post, it should clarify, I hope. :-)

idfer

12:43 am on Sep 28, 2009 (gmt 0)

10+ Year Member



This is what i usually do to find out the absolute path name to a directory on a web server:

- Write a PHP script, let's call it fish.php, that generates an error, e.g.:

<?php
error_reporting(E_ALL);
require_once 'badfile';
?>

- Put this script in the target directory and call it from a browser. You should get a message like:

Fatal error: require_once() [function.require]: Failed opening required 'badfile' (include_path='.;...') in /foo/bar/public_html/baz/fish.php on line 3

- Everything in bold preceding fish.php is the absolute path to that directory. In your particular script, you would set $image_dir to that value.

- If no error messages show up, try looking in your host's error log.

** Important: Remove the file from your web server afterwards.

If you continue to have problems, try adding this before the call to move_uploaded_file():

echo '<pre>imageName='.$imageName.'</pre>';
echo '<pre>_FILE=';
print_r($_FILES['image_filename']['tmp_name']);
echo '</pre>';

BTW, a textbook is a great start, but you will also want to use the online PHP reference manual at [php.net...] It'll save you a lot of head scratching.