me1000 Posted January 27, 2008 Share Posted January 27, 2008 //$file is the name of the file field in the form. //$httpPost is the $HTTP_POST_FILES variable. //$thumbnail is either blank(missing) or 1 to trigger a thumbnail. //You get an array back with all the data ready for insertion into a MySQL table. function uploadFile ($httpPost, $file, $thumbnail='1') { $fileUpload_tmp_name = $httpPost[$file]['tmp_name']; $fileUpload_name = $httpPost[$file]['name']; $fileUpload_size = $httpPost[$file]['size']; $fileUpload_type = $httpPost[$file]['type']; if ($fileUpload_tmp_name) { $oData = array(); $oData['data'] = addslashes(fread(fopen($fileUpload_tmp_name, "r"), filesize($fileUpload_tmp_name))); $oData['size'] = $fileUpload_size; $oData['type'] = $fileUpload_type; if ($thumbnail) $oData['thumbnail'] = $this->createThumbnail($fileUpload_tmp_name); unlink($fileUpload_tmp_name); return $oData; } } function createThumbnail ($tmp_file) { $tmp_thumb = $tmp_file . ".thumb"; exec("convert $tmp_file -quality 70 -scale 150 $tmp_thumb"); $output = addslashes(fread(fopen($tmp_thumb, "r"), filesize($tmp_thumb))); unlink($tmp_thumb); return $output; } There are obviously 2 functions there, but I need to call the uploadFile() function and I am unsure how to do it. the input field is... <input type="file" name="image" /> Please let me know if you need any more information because this is pretty important! Thank You, Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted January 27, 2008 Share Posted January 27, 2008 How did you get the code in the first place? Looking at the code the two functions uploadFile and createThumbnail are part of a class. You will need to initiate the class first and then call the uploadFile method (the createThumbnail method gets called by uploadFile method). Quote Link to comment Share on other sites More sharing options...
me1000 Posted January 27, 2008 Author Share Posted January 27, 2008 The code was supplied to me by a friend who was trying to help me out on file uploads. Can you be more specific on how this is done? Thank You, Quote Link to comment Share on other sites More sharing options...
nikefido Posted January 27, 2008 Share Posted January 27, 2008 How did you get the code in the first place? Looking at the code the two functions uploadFile and createThumbnail are part of a class. You will need to initiate the class first and then call the uploadFile method (the createThumbnail method gets called by uploadFile method). Looks like he took the functions out of someones class and just changed them into process-type function (probably missed one of the "$this->"'s in there). Anyway, to answer your question, you should just be able to change "$this->createThumbnail($fileUpload_tmp_name);" to: createThumbnail($fileUpload_tmp_name); and it will work. Calling a function is as simple as: uploadFile($var1, $var2); //you have to set $var1 and $var2 to the proper data Quote Link to comment Share on other sites More sharing options...
me1000 Posted January 27, 2008 Author Share Posted January 27, 2008 So when i try to call the function I am using this code... if ($_REQUEST['submit']){ uploadFile($HTTP_POST_FILES, 'form_data'); echo $oData['type']; } the echo is just to see if anything worked. I believe the 2nd variable is right, however I dont think the 1st one is. this is the whole form... <form method="post" action="uploadtest.php" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> <input type="file" name="form_data" size="40"> <p><input type="submit" name="submit" value="submit"> </form> Quote Link to comment Share on other sites More sharing options...
me1000 Posted January 28, 2008 Author Share Posted January 28, 2008 *bump* Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted January 29, 2008 Share Posted January 29, 2008 So when i try to call the function I am using this code... if ($_REQUEST['submit']){ uploadFile($HTTP_POST_FILES, 'form_data'); echo $oData['type']; } the echo is just to see if anything worked. I believe the 2nd variable is right, however I dont think the 1st one is. this is the whole form... <form method="post" action="uploadtest.php" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> <input type="file" name="form_data" size="40"> <p><input type="submit" name="submit" value="submit"> </form> $HTTP_POST_VARS has been depreciated, use $_POST instead Also functions only return variables values, they do not return the actual variable. You'll have to catch the returned value by assigning a variable to the function call, example: $oData = uploadFile($_POST, 'form_data'); Any data that uploadFile will return will be assigned to the $oData variable. Quote Link to comment Share on other sites More sharing options...
me1000 Posted February 2, 2008 Author Share Posted February 2, 2008 im getting several errors back, but they are all saying the same thing, Notice: Undefined index: form_data in... but if I use $_FILES instead of $_POST I get these errors, Warning: fopen(/Applications/MAMP/tmp/php/phpgjGUYv.thumb) [function.fopen]: failed to open stream: No such file or directory in /Users/randy/Sites/uploadtest2.php on line 30 Warning: filesize() [function.filesize]: stat failed for /Applications/MAMP/tmp/php/phpgjGUYv.thumb in /Users/randy/Sites/uploadtest2.php on line 30 Warning: fread(): supplied argument is not a valid stream resource in /Users/randy/Sites/uploadtest2.php on line 30 Warning: unlink(/Applications/MAMP/tmp/php/phpgjGUYv.thumb) [function.unlink]: No such file or directory in /Users/randy/Sites/uploadtest2.php on line 31 any ideas? Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.