Jump to content

[SOLVED] Calling a function


me1000

Recommended Posts

//$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,

 

 

Link to comment
Share on other sites

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).

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.