Page 1 of 1

Still Need Help With Image Upload Script

Posted: Fri Jul 12, 2002 1:25 am
by toriboy
I am not a programmer so if possible I need some pretty basic and detailed help on this. I am very very new to this.

I currently use a script to replicate html documents, it replicates the page by someone filling in an html form, the script put variables from the form into the new html page (EXAMPLE: if I put $Name in the template, the script would then put the "name" from the HTML form on the new html document).

The script has always worked fine because I never needed images just to replicates the page with some customization.

But now I need to put a few images on the html page (the user will upload the images via the form).

The script uloaded the files to the directory (wich is also taken from the html form) but I don't know how to make the "template" so the images show up on the new html page, the images are uploaded via a form so I do not know what the images names are. so I can't just put an image tag.

If I put $name in the template , then the name from the html form is put on the replicated page, I have 6 images on the form, how can I cange the code below, or the form , or whatever, so that I can put something like $image1 , $image2, etc on the tempalte and have it work.

Below is the php code and the html form code.

Please someone help. I am getting very frustrated!

Thank You In Advance.


The PHP Code Is
----------------------------

if (!file_exists($UserName)) {
mkdir("$UserName",0777);
chmod ($UserName,0777);
}elseif(!file_exists($AltUserName)) {
mkdir("$AltUserName",0777);
chmod ($AltUserName,0777);
$username = $AltUserName;
}else{ // error and back to main sign up page
$errors[] = "Both your chosen 'username and your alternative username are taken, please choose different ones.";
error_log("[PHPFormMail] Username already taken.");
$output .= "<p>Both your chosen 'username and your alternative username are already taken, please choose different ones. Please use the <a href=\"javascript: history.back();\">back</a> button to correct these errors.</p>\n";
output_html($output);
exit;
}
// does the directory exist ?
$DataPath = $username;
$DataPath .= "/";
$DataPath .= "index.html";
$myfile = fopen("$DataPath","w");

/********************************************/

$nPics = (count($pictures));

for($i = 0; $i < $nPics; $i++) {

if(stristr($HTTP_POST_FILES['pictures']['type'][$i], 'image/')){ // only accept picture files

if (is_uploaded_file($HTTP_POST_FILES['pictures']['tmp_name'][$i]))
copy($HTTP_POST_FILES['pictures']['tmp_name'][$i],
"$username/" . basename($HTTP_POST_FILES['pictures']['name'][$i]));
}
}
/********************************************/

$Body = "<html><head><title>Web Sites For Kwik Realtor</title><meta http-equiv=Content-Type content='text/html; charset=iso-8859-1'></head>
<body>
My HTML Template
</body></html>";

----------------------------

The html code for the form that makes the pages is below:
----------------------------

<form name="form1" method="post" enctype="multipart/form-data" action="replicate.php">

Name : <input type="text" name="realname" size="25" maxlength="100">
Email : <input type="text" name="email" size="25" maxlength="100">
City / Town: <input name="City" type="text" id="City" size="25" maxlength="100">
Username : <input type="text" name="username" size="20" maxlength="15">
Picture 1 : <input name="pictures[]" type="file" accept="image/gif,image/jpeg, image/png">
Picture 2 : <input name="pictures[]" type="file" accept="image/gif,image/jpeg, image/png">
Picture 3 : <input name="pictures[]" type="file" accept="image/gif,image/jpeg, image/png">
Picture 4 : <input name="pictures[]" type="file" accept="image/gif,image/jpeg, image/png">
Picture 5 : <input name="pictures[]" type="file" accept="image/gif,image/jpeg, image/png">
Picture 6 : <input name="pictures[]" type="file" accept="image/gif,image/jpeg, image/png">

</form>
-------------------------

Posted: Fri Jul 12, 2002 12:06 pm
by RandomEngy
The problem is that you can't assume the user has uploaded all 6 pictures. Assume you did something like this:

Code: Select all

Here is picture 1:<br>
$image1 <br>
Here is picture 2:<br>
$image2 <br>
...
Say the user uploads only 2 pictures. Then you'll get a lousy output like this:

Here is picture 1:
<picture here>
Here is picture 2:
<picture here>
Here is picture 3:

Here is picture 4:

Here is picture 5:

Here is picture 6:

Anyway, keep in mind that when you put in $image4 that it might not actually show up.

Put this code before the $Body line:

Code: Select all

if( $HTTP_POST_FILES&#1111;'pictures']&#1111;'size']&#1111;0] != 0 )
  $image1 = "<img src="".$HTTP_POST_FILES&#1111;'pictures']&#1111;'name']&#1111;0]."">";
if( $HTTP_POST_FILES&#1111;'pictures']&#1111;'size']&#1111;1] != 0 )
  $image2 = "<img src="".$HTTP_POST_FILES&#1111;'pictures']&#1111;'name']&#1111;1]."">";
if( $HTTP_POST_FILES&#1111;'pictures']&#1111;'size']&#1111;2] != 0 )
  $image3 = "<img src="".$HTTP_POST_FILES&#1111;'pictures']&#1111;'name']&#1111;2]."">";
if( $HTTP_POST_FILES&#1111;'pictures']&#1111;'size']&#1111;3] != 0 )
  $image4 = "<img src="".$HTTP_POST_FILES&#1111;'pictures']&#1111;'name']&#1111;3]."">";
if( $HTTP_POST_FILES&#1111;'pictures']&#1111;'size']&#1111;4] != 0 )
  $image5 = "<img src="".$HTTP_POST_FILES&#1111;'pictures']&#1111;'name']&#1111;4]."">";
if( $HTTP_POST_FILES&#1111;'pictures']&#1111;'size']&#1111;5] != 0 )
  $image6 = "<img src="".$HTTP_POST_FILES&#1111;'pictures']&#1111;'name']&#1111;5]."">";
Then use $image1, $image2, etc in the template.

And next time, just make one post about it, and reply to it to bump it to the top if you MUST have an answer.

Thanks RandomEngy

Posted: Fri Jul 12, 2002 2:05 pm
by toriboy
Thank you very much for the reply, I will try what you said right now,

Sorry about the multible posts.