Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2006-03-27 23:53:20

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Front end image upload.

May be a stupid question but…
Is there any way (plugin or no) to allow site visitors to upload images via a form to a TXP install?

Offline

#2 2006-03-28 02:01:32

soulship
Member
From: Always Sunny Charleston
Registered: 2004-04-30
Posts: 669
Website

Re: Front end image upload.

I had one in a post, but it looks as if that has been deleted from the forum db- :(

I found it in a google cache, but it wont let me get to page two of the post. :(

Let me look through my old laptop and I will upload it again so I have a place to find it as well ;)

Offline

#3 2006-03-28 02:05:36

soulship
Member
From: Always Sunny Charleston
Registered: 2004-04-30
Posts: 669
Website

Re: Front end image upload.

OK….

<code>
<?php
$emailaddress = “you@yourdomain.com”;
$this_script = “$PHP_SELF”;
$home_page = “http://yourdomain.org”;
$uploaddir = $_SERVER[‘DOCUMENT_ROOT’].”/incoming/”;
$type = array(“.jpg”,”.gif”,”.png”,”.pdf”,”.tif”,”.tiff”,”.eps”); // enter in all lower case
$maxSize = 10000000;
$maxDisplay = $maxSize / 1000000;
$maxFileSpace = 50000000;
$error_class = “error”; //hook for css styles – error styling
$success_class = “success”;
?>
<div class=“upload”>
<?php
// print_r($_FILES); // print to be used for debugging
$file_name = $_FILES[‘file’][‘name’];
$file_size = $_FILES[‘file’][‘size’];
$file_tmp_name = $_FILES[‘file’][‘tmp_name’];
if ($file_name) { $error = “”; echo “<ul><li><a>File Name: $file_name</a></li></ul>”; echo “<ul><li><a>File Size: $file_size<br/></a></li></ul>”; // file size test if ($file_size 0 ) $error .= "Invalid file"; if ($file_size > $maxSize ) $error .= "Your file exceeds $maxDisplay mb."; // file type test $type_test = strtolower(strstr($file_name, '.')); if (!in_array($type_test, $type) ) $error .= "Your file is not a valid file type."; // max directory size test if ($dir = @opendir("$uploaddir")) { while (($file_select = readdir($dir)) ! false) { $type_test = strtolower(strstr($file_select, ‘.’)); if (in_array($type_test,$type)) { $file_size_accum = filesize(“$uploaddir$file_select”); $total_size = $total_size + $file_size_accum; } } closedir($dir); } if (($total_size+$file_size) >= $maxFileSpace) $error .= “Total file space limits have been exceeded.”; // eliminate bad characters from the file name $file_name = stripslashes($file_name); $file_name = preg_replace(“#[ ]#”,”_”,$file_name); // change spaces to underscore $file_name = preg_replace(‘#[^()\.\-,\w]#’,’_’,$file_name); //only parenthesis, underscore, letters, numbers, comma, hyphen, period – others to underscore $file_name = preg_replace(‘#()+#’,’’,$file_name); //eliminate duplicate underscore // check for file already exists if (file_exists(“$uploaddir$file_name”)) $error .= “File already exists.”; // if all is valid, do the upload if ($error == “”) { if (move_uploaded_file($file_tmp_name, “$uploaddir$file_name”)) { chmod(“$uploaddir$file_name”, 0644); echo “<ul><li class=\”$success_class\”>Your file was successfully uploaded!</a></li></ul>”; mail($emailaddress, “You have a file upload” , $file_name, “From: Upload <>”); } else { echo “<ul><li class=\$error_class\”>Your file could not be uploaded.</li></ul>”; } } echo “<ul><li><a class=\”$error_class\”>$error</a></li></ul>”;
} else { echo “”;
}

?>
Upload a
<?php
foreach($type as $print_type) { echo $print_type; }
?> file to our server.<br/><div class=“filesize”><?=$maxDisplay?> mb </div>
Maximum file size is <?=$maxDisplay?> mb
<div class =“uploads”><form action=”<?=$this_script?>” method=“post” enctype=“multipart/form-data”>
<p>File:</p><fieldset><div class=“fileinputs”><input type=“file” class=“file” name=“file” size=“20” /></div></fieldset>
<fieldset class=“upcon”><input class=“uploadit” type=“submit” name=“submit” value=“Upload File” /> </fieldset></form></div>
<a class=“return” href=”<?=$home_page?>”>Return to the Home Page</a>
</div>

</code>

I have not used it in a bit so it needs to be dusted off a bit. You need a folder chmod to 777 called incoming. It is not super sophisticated, but works well and if you are on textdrive you can pump up to a 40mb file through php.

Offline

#4 2006-03-28 02:07:14

soulship
Member
From: Always Sunny Charleston
Registered: 2004-04-30
Posts: 669
Website

Re: Front end image upload.

There is also jotform if the images are going to be small. I think the limit is 10mb per account so you have to be dilligent about retrieving files.

Offline

#5 2006-03-28 02:14:35

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Re: Front end image upload.

Groovy!

A wealth of info, and all I had to do was ask.

Thanks big, Soulship.

Offline

Board footer

Powered by FluxBB