Jump to content

Upload and display image


acctman

Recommended Posts

normally you would be able to find this on the phpfreaks main page, but since it is offline for maintenance reasons..

 

<?php
// http://be2.php.net/manual/en/features.file-upload.php#features.file-upload.post-method
$post_name = 'userfile';
$where_to = ''; // path to upload directory (no trailing slash)

if (strcmp(strtolower($_SERVER['REQUEST_METHOD']), 'post') === 0) {
    if (array_key_exists($post_name, $_FILES)) {
        $upload = $_FILES[$post_name];
        $where_to = implode(DIRECTORY_SEPARATOR, array($where_to, $upload['name']));
        if (is_uploaded_file($upload['tmp_name'])) {
            if (move_uploaded_file($upload['tmp_name'], $where_to)) {
                printf("<img src=\"%s\" width=\"100\" height=\"100\" />", $where_to);
            } else {
                // error
            }
        } else {
             // nothing uploaded
        }
    } else {
         // $post_name not present in $_FILES
    }
}
?>

Link to comment
Share on other sites

is there a way to combine the above example code with the coding below and make everything work together? pretty much i want everything to work so that uploading and displaying of the image is all done on the same page

 

<form name="form1" method="post" action="" enctype="multipart/form-data"> 
	<input size=50 type=file name=img><br>
	<input name="Submit" type="submit" value="Upload">

<?php
// if(isset( $Upload )) 
if($_POST["action"] == "Upload")
{

// ==============
// Configuration
// ==============
$uploaddir = "temp"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!
$allowed_ext = "jpg, jpeg, gif, png, bmp"; // These are the allowed extensions of the files that are uploaded
$max_size = "2048000"; // 2 megabyte
$max_height = "480"; // This is in pixels - Leave this field empty if you don't want to upload images
$max_width = "640"; // This is in pixels - Leave this field empty if you don't want to upload images 
// Check Entension
$extension = pathinfo($_FILES['file']['name']);
$extension = $extension[extension];
$allowed_paths = explode(", ", $allowed_ext);
for($i = 0; $i < count($allowed_paths); $i++) {
if ($allowed_paths[$i] == "$extension") {
$ok = "1";
}
}

// Check File Size
if ($ok == "1") {
if($_FILES['file']['size'] > $max_size)
{
print "File size is too big!";
exit;
}

// Check Height & Width // need to resize file 
if ($max_width && $max_height) {
list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']);
if($width > $max_width || $height > $max_height)
{
print "File height and/or width are too big!";
exit;
}
}

// The Upload Part
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
}
print "Your file has been uploaded successfully! Yay!";
} else {
print "Incorrect file extension!";
}
?>

</form>

Link to comment
Share on other sites

action="<?php echo $_SERVER['PHP_SELF']; ?>" 

 

Something else might not work, but make the form reload the page. If this doesn't work, look in the php code for other possible solutions.

 

how much would you charge to code this correctly for me? combine my code with ignace's code for displaying image after upload. I've been at this for a few days i'd like to move on to something else.

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.