Jump to content

Image Upload


supanoob

Recommended Posts

i have used a tutorial for this but i cant seem to get it working, basically it is showing the image as having no file extension

 

this his the code:

<?php 

function getFileExtension($str) {

        $i = strrpos($str,".");
        if (!$i) { return ""; }

        $l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);

        return $ext;

    }

if ($screenshot == 'on')
{

    /* SUBMITTED INFORMATION - use what you need
     * temporary filename (pointer): $imgfile
     * original filename           : $imgfile_name
     * size of uploaded file       : $imgfile_size
     * mime-type of uploaded file  : $imgfile_type
     */

     /*== upload directory where the file will be stored 
          relative to where script is run ==*/
    
    $uploaddir = "../thehub/images/webshots/";
    

    /*== get file extension (fn at bottom of script) ==*/
    /*== checks to see if image file, if not do not allow upload ==*/
    $pext = getFileExtension($imgfile_name);
    $pext = strtolower($pext);
    if (($pext != "jpg")  && ($pext != "jpeg")  && ($pext != "gif"))
    {
        print "<h1>ERROR</h1>Image Extension Unknown.<br>";
        print "<p>Please upload only a JPEG image with the extension .jpg, .jpeg or .gif<br><br>";
        print "The file you uploaded had the following extension: $pext</p>\n";

        /*== delete uploaded file ==*/
        unlink($imgfile);
        exit();
    }


    //-- RE-SIZING UPLOADED IMAGE

    /*== only resize if the image is larger than 250 x 200 ==*/
    $imgsize = GetImageSize($imgfile);

    /*== check size  0=width, 1=height ==*/
    if (($imgsize[0] > 500) || ($imgsize[1] > 500)) 
    {
        /*== temp image file -- use "tempnam()" to generate the temp
             file name. This is done so if multiple people access the 
            script at once they won't ruin each other's temp file ==*/
        $tmpimg = tempnam("/tmp", "MKUP");

        /*== RESIZE PROCESS
             1. decompress jpeg image to pnm file (a raw image type) 
             2. scale pnm image
             3. compress pnm file to jpeg image
        ==*/
        
        /*== Step 1: djpeg decompresses jpeg to pnm ==*/
        system("djpeg $imgfile >$tmpimg");
        

        /*== Steps 2&3: scale image using pnmscale and then
             pipe into cjpeg to output jpeg file ==*/
        system("pnmscale -xy 500 500 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");

        /*== remove temp image ==*/
        unlink($tmpimg);

    }

    /*== setup final file location and name ==*/
    /*== change spaces to underscores in filename  ==*/
    $final_filename = str_replace(" ", "_", $imgfile_name);
    $newfile = $uploaddir . "/$final_filename";
    
    /*== do extra security check to prevent malicious abuse==*/
    if (is_uploaded_file($imgfile))
    {

       /*== move file to proper directory ==*/
       if (!copy($imgfile,"$newfile")) 
       {
          /*== if an error occurs the file could not
               be written, read or possibly does not exist ==*/
          print "Error Uploading File.";
          exit();
       }
     }

    /*== delete the temporary uploaded file ==*/
    unlink($imgfile);

    
    print("<img src=\"$final_filename\">");

    /*== DO WHATEVER ELSE YOU WANT
         SUCH AS INSERT DATA INTO A DATABASE  ==*/

}
else
{
$final_filename = 'no_screenshot.jpg';
}

?>

 

and this is whats output:

ERROR

Image Extension Unknown.

Please upload only a JPEG image with the extension .jpg, .jpeg or .gif

 

The file you uploaded had the following extension:

 

Any help will be much appreciated

 

 

Link to comment
Share on other sites

Where is the code that "$imgfile_name" is being set from?

 

If it's loaded from a form, be sure you are setting it from a $_POST or $_GET from somewhere.

 

its being sent from

 


<form method=\"post\" action=\"index.php?step=websites_process\" enctype=\"multipart/form-data\">
<table style=\"width: 100%\">
<tr>
		<td colspan=\"2\" class=\"desc\">Please enter the name of the website you would like to submit. Along with its URL and a brief descrption of the site, the can be upto 500 words, but no less than 50.</td>
	</tr>
	<tr>
		<td style=\"width: 254px\">Website Name:</td>
		<td><input name=\"name\" type=\"text\" size=\"30\" /></td>
	</tr>
	<tr>
		<td style=\"width: 254px\">Website URL (with the http://):</td>
		<td><input name=\"url\" type=\"text\" size=\"60\" value=\"http://\" /></td>
	</tr>
	<tr>
		<td style=\"width: 254px\">Website Description:</td>
		<td><textarea name=\"description\" cols=\"30\" rows=\"5\"></textarea></td>
	</tr>
	<tr>
		<td colspan=\"2\" class=\"desc\">Please select a catagory that you feel this website falls under.</td>
	</tr>
	<tr>
		<td style=\"width: 254px\">Website Catagory</td>
		<td><select name=\"website_catagory\">
		<option value=\"Adult\">Adult </option>
      <option value=\"Advertising\">Advertising </option>
      <option value=\"Auto\">Auto </option>
      <option value=\"Business\">Business </option>
      <option value=\"Chat\">Chat</option>
      <option value=\"Cities\">Cities </option>
      <option value=\"Education\">Education </option>
      <option value=\"Employment/Jobs\">Employment/Jobs </option>
      <option value=\"Entertainment\">Entertainment</option> 
      <option value=\"Events\">Events</option>
      <option value=\"Forums\">Forums</option>
      <option value=\"Games\">Games </option>
      <option value=\"Holidays\">Holidays </option>
      <option value=\"Health\">Health </option>
      <option value=\"Horoscope\">Horoscope </option>
      <option value=\"Internet\">Internet </option>
      <option value=\"Marketing\">Marketing </option>
      <option value=\"Networking\">Networking </option>
      <option value=\"People\">People </option>
      <option value=\"Places\">Places </option>
      <option value=\"Real Estate\">Real Estate</option>
      <option value=\"Search\">Search</option> 
      <option value=\"Science\">Science </option>
      <option value=\"Shopping\">Shopping </option>
      <option value=\"Sports\">Sports </option>
      <option value=\"Technology\">Technology </option>
      <option value=\"Travel\">Travel </option>
      <option value=\"Weather\">Weather</option>
		</select></td>
	</tr>
	<tr>
		<td colspan=\"2\" class=\"desc\">You may choose to upload a screenshot of the website you are submitting. This will allow users to see what website they are going to before they go to the site. Please tick the following box if you wish to do so. <input name=\"screenshot\" type=\"checkbox\" /></td>
	</tr>
	<tr>
		<td style=\"width: 254px\">Upload:<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"50000\"></td>
		<td>Select file: <input name=\"imgfile\" type=\"file\" id=\"ufile\" size=\"50\" /></td>
	</tr>
	<tr>
	<tr>
		<td colspan=\"2\" class=\"desc\">By clicking submit you agree that the website you are submitting does not break any terms and conditions, you also agree that you have read the privacy policy regarding submitions of websites (part ##)</td>
	</tr>
	<tr>
		<td style=\"width: 254px\"> </td>
		<td><input name=\"Submit1\" type=\"submit\" value=\"submit\" class=\"button\"/></td>
	</tr>
</table>
</form>

 

which is using POST

Link to comment
Share on other sites

Sorry, it's this I believe:

$imgfile_name = $_FILES['imgfile']['name'];

 

You would put that on the page before the function "getFileExtension".

 

that fixed that error now i have a problem it isnt placing the image into the directory its supposed to, it isnt copying it to any directory

Link to comment
Share on other sites

<?php
$imgfile = $_FILES['imgfile']['tmp_name'];
?>

 

Try that, and please don't use scripts from people who don't provide instructions, otherwise read instructions provided.

 

the other one you gave me worked, fixed the no extension error, its just the file is not copying over into the directory now, but i think i know why.

Link to comment
Share on other sites

I know what worked and what didn't. What I just gave you is in addition to it. The script doesn't know where to get the old file from. When you use the file upload, it puts it in a temporary location. That will set the $imgfile variable, which the script thinks is the temporary location, to actually HAVE the temporary location contained in $_FILES['imgfile']['tmp_name'].

Link to comment
Share on other sites

I know what worked and what didn't. What I just gave you is in addition to it. The script doesn't know where to get the old file from. When you use the file upload, it puts it in a temporary location. That will set the $imgfile variable, which the script thinks is the temporary location, to actually HAVE the temporary location contained in $_FILES['imgfile']['tmp_name'].

 

so what exactly do i do with what you gave me?

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.