Jump to content

PHP upload file help


danoush

Recommended Posts

Im totally new to PHP, and ive been looking through a couple tutorials to try and get a picture upload form on my website. All i want it to do for now is to allow anybody to upload pictures into a specified folder on my host server.

 

heres the PHP i have

<?php

 

$err_upload = "";

 

$image_file = $_FILES[’upload_file’];

$temporary_name = $image_file[’tmp_name’];

 

if (is_uploaded_file($temporary_name)) {

 

// get image information

$image_metadata = getimagesize($temporary_name);

 

if ($image_metadata) {

$image_width = $image_metadata[0];

$image_height = $image_metadata[1];

$image_type = $image_metadata[2];

 

$save_name = $temporary_name;

 

switch ($image_type)

{

case IMAGETYPE_GIF:

$save_name .= ".gif";

break;

case IMAGETYPE_PNG:

$save_name .= ".png";

break;

case IMAGETYPE_JPG:

$save_name .= ".jpg";

break;

case IMAGETYPE_JPEG:

$save_name .= ".jpeg";

break;

default:

$err_upload = "Sorry… we only allow gif, png, jpg, and jpeg images.";

break;

}

 

if (! $err_upload) {

if (move_uploaded_file($tmp_name, "/images/$tmp_name")) {

// you might update a database with key information here so

// that the image can be used later

} else {

$err_upload = "Sorry… something didn't work.";

}

}

}

 

} else {

 

// some error occurred: handle it

switch ($image_file[’error’])

{

case 1: // file too big (based on php.ini)

case 2: // file too big (based on MAX_FILE_SIZE)

$err_upload = "Sorry… image too big, try resizing.";

break;

case 3: // file only partially uploaded

case 4: // no file was uploaded

case 6: // missing a temporary folder

case 7: // failed to write to disk (only in PHP 5.1+)

$err_upload = "Sorry… failed to upload… problem with server.";

break;

}

}

 

if ($err_upload) {

print $err_upload;

} else {

print "Success!!!";

}

?>

 

And the HTML that goes along with it

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Upload Your Images</title>

</head>

<body>

 

<form method='POST' name='my_form' enctype='multipart/form-data' action='do_upload.php'>

 

<input type='hidden' name='MAX_FILE_SIZE' value="100000000" />

Which image? <input name="upload_file" type="file" />

<br /><br />

<input type="submit" value="Send Selected Image To Servers" />

 

</form>

 

</body>

</html>

 

I also 777'ed basically... everything to try and get it to work. I dont understand which files need permissions (or folders)

 

When i run it on my server it says Success! but the picture is nowhere to be found.

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/107549-php-upload-file-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.