View Full Version : php script to upload jpg and create custom html file

06-30-2005, 06:46 PM
I've started looking for scripts some today and thought I'd post here to see if anyone could point me in a good direction..

I need a php script that'll allow my co-worker to upload a jpg image to our webserver from a simple html form. The catch is, I also need the script to create a new html file in the same directory, with the same name as the jpg, and have links to the jpg file inside the html file.

For example, my co-worker uploads a file called proof001.jpg to our server via html form, and with the help of php, a new html file is created in the same web directory called proof001.html, and within the code of the new html file, there is the line <img src="proof001.jpg"> among some more code that'll make up the webpage.

I hope this is clear enough to understand, and hope that it very well is possible. So if anyone knows where this might currently exist, or like I said, could point me in the right direction of finding this code, please do so. Or, if this is simple enough to quickly right up, do please share. Thanks.

07-01-2005, 01:30 PM
It shouldn't be too hard to write something to do what you want. Here's some basic skeleton code that will handle the file upload, check it has the correct filename extension, create a html page with the title as the name of the file and the file itself displayed via the <img> tag.

if(!isset($_POST) || empty($_POST)) {
<form name="upload_form" enctype="multipart/form-data" method="post" action="<?php echo basename($_SERVER['PHP_SELF']); ?>">
<input type="file" name="userfile">
<input type="submit" name="Submit" value="Submit">

} else {

// function to return filename and extension as an array
function split_extension($filename){
for($i = 1; $i < strlen($filename); $i++){
if(substr($filename, -$i, 1) == '.'){
$filename_array['filename'] = substr($filename, 0, -$i);
$filename_array['extension'] = substr($filename, -$i, strlen($filename));
return $filename_array;

if(isset($_FILES['userfile']) && !empty($_FILES['userfile'])) {

// check the filename has the correct extension
$allowed_extensions = array('.jpg', '.gif', '.png');
$filename_array = split_extension($_FILES['userfile']['name']);
if(!in_array($filename_array['extension'], $allowed_extensions)) {
echo 'You can only upload .gif, .jpg or .png files!';

// move the uploaded file to the target destination
$uploaddir = '/path/to/directory/';
$uploadname = $_FILES['userfile']['name'];
if(!move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $uploadname)) {
echo 'The file could not be uploaded this time. <a href="' . basename($_SERVER['PHP_SELF']) . '>Click here and try again.</a>';

// create the html for the page
$html = ' <html>
<title>' . $filename_array['filename'] . '</title>
<img src="' . $_FILES['userfile']['name'] . '">

// create the html file and insert the html into it
if(!$handle = fopen($uploaddir . $filename_array['filename'] . '.htm', 'w')) {
echo "Cannot open file.";
if(fwrite($handle, $html) === FALSE) {
echo "Cannot write to file.";
echo 'Image uploaded and html file successfully created';
Obviously it'll need some expanding to handle what you want, there's no checking whether the upload returns an error or files exist with the selected names etc and the directory needs to be chmodded to 0777 for it to work, but it literally took 10 minutes so expanding upon it should be pretty simple.

07-01-2005, 10:07 PM
PERRRRRFECT!! This worked out great for me and I've got it all set up on my site exactly the way i wanted it. Thank you. I'll be sure to reply back if I've got any questions in the future about this but so far its flawless.