1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Upload Script

Discussion in 'PHP' started by dean5000v, May 1, 2008.

  1. #1
    ok ive done this php upload script, which simply uploads the file to my uploads directory, i wont to be able to add into a function where it sets the a link to the image into a variable and then prints it. here is my code

    <?php
    $maxsize=28480; //set the max upload size in bytes
    if (!$HTTP_POST_VARS['submit']) {
    //print_r($HTTP_POST_FILES);
    $error=" ";
    //this will cause the rest of the processing to be skipped
    //and the upload form displays
    }
    if (!is_uploaded_file($HTTP_POST_FILES['upload_file']['tmp_name']) AND
    !isset($error)) {
    $error = "<b>You must upload a file!</b><br /><br />";
    unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
    }
    if ($HTTP_POST_FILES['upload_file']['size'] > $maxsize AND !isset($error)) {
    $error = "<b>Error, file must be less than $maxsize bytes.</b><br /><br />";
    unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
    }
    if($HTTP_POST_FILES['upload_file']['type'] != "image/gif" AND
    $HTTP_POST_FILES['upload_file']['type'] != "image/pjpeg" AND
    $HTTP_POST_FILES['upload_file']['type'] !="image/jpeg" AND !isset($error)) {
    $error = "<b>You may only upload .gif or .jpeg files.</b><br /><br />";
    unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
    }
    if (!isset($error)) {
    	move_uploaded_file($HTTP_POST_FILES['upload_file']['tmp_name'], "uploads/".$HTTP_POST_FILES['upload_file']['name']);
    	print "thank you for your upload.";
    	exit;
    	}
    	else
    	{
    		echo ("$error");
    	}
    	
    	
    	
    ?>
    	<html>
    <head></head>
    <body>
    <form action="<?php echo(htmlspecialchars($_SERVER['PHP_SELF']))?>" method="post"
    enctype="multipart/form-data">
    Choose a file to upload:<br />
    <input type="file" name="upload_file" size="80">
    <br />
    <input type="submit" name="submit" value="submit">
    </form>
    </body>
    </html>
    PHP:

     
    dean5000v, May 1, 2008 IP
  2. CPURules

    CPURules Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    if (!isset($error)) {
        move_uploaded_file($HTTP_POST_FILES['upload_file']['tmp_name'], "uploads/".$HTTP_POST_FILES['upload_file']['name']);
        print "thank you for your upload.";
        exit;
        }
    
    PHP:
    That's part of your script. It needs to be this:
    
    if (!isset($error)) {
        move_uploaded_file($HTTP_POST_FILES['upload_file']['tmp_name'], "uploads/".$HTTP_POST_FILES['upload_file']['name']);
        print "thank you for your upload.<br />";
        print "<a href=\"uploads/" . $HTTP_POST_FILES['upload_file']['tmp_name'] . "\">View your image</a>";
        print "(or access it directly: http://www.YOURWEBSITENAME.com/uploads/" . $HTTP_POST_FILES['upload_file']['tmp_name'] . ")";
        exit;
        }
    
    PHP:
    Obviously, change YOURWEBSITENAME.COM to your website, and if this upload script is not in the root DIR of the website, you'll need to change YOURWEBSITENAME.COM to YOURWEBSITENAME.COM/DIR, where DIR is the directory that this script resides in.

    Sorry if that was a bit confusing :3
     
    CPURules, May 1, 2008 IP
  3. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    nope wasn't confusing at all, thanks a lot for the example !!
     
    dean5000v, May 1, 2008 IP
  4. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ive tried adding in the script and the first link is just basically a link to the uploads file which i don't want, and the second one just gives me a link of the temp file php has given it which doesn't work.

    the script uploads the file to my uploads file but i just want it so gives me a link the the exact file name in the folder e.g. uploads/image1.jpg

    mybe im just being stupid though
     
    dean5000v, May 1, 2008 IP
  5. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    sorry for doing a third post but i have figured it, wasnt hard im just crap lol. heres the code.

     print "<a href=\"http://www.mydomain.com/uploads/" . $HTTP_POST_FILES['upload_file']['name'] . "\">View your image</a>";
    PHP:
     
    dean5000v, May 1, 2008 IP
  6. DarkMindZ

    DarkMindZ Guest

    Messages:
    175
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    DarkMindZ, May 1, 2008 IP