View Full Version : upload a picture using php
BlackBeltNinja
March 14th, 2005, 08:51 PM
okay,
i've searched the internet and found MANY tutorials that show how to upload files using php. The problem is that i can't get any of the tutorials to work. i know how to use mysql and write files (like txt) but i can't upload. plz someone help.
abcdefg
March 14th, 2005, 09:38 PM
1. Maybe your server allows you to move files?(permissions)
2. Are you sure your uploading the files to a directory(check your path)
b.rich
March 15th, 2005, 12:01 AM
what happens when you try the upload script?
Yeldarb
March 15th, 2005, 12:22 AM
Make sure that in your form tag you have:
enctype="multipart/form-data"
Jerryscript
March 15th, 2005, 11:39 AM
Here is a very basic upload script, if it doesn't work for you, please post any error messages shown on the page or in your error logs:
<?
if($_FILES){
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
$uploaddir = substr(0,strrpos("/",$SCRIPT_NAME),$SCRIPT_NAME);
$uploadfile = $uploaddir . $_FILES['userfile']['name'];
$info=getImageSize($_FILES['userfile']['tmp_name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
print "File is valid, and was successfully uploaded.<br>";
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}
}else{
print "
<form enctype='multipart/form-data' method='post'>
<input type='hidden' name='MAX_FILE_SIZE' value='30000' />
Send this file: <input name='userfile' type='file' />
<input type='submit' value='Send File' />
</form>
";
}
?>
BlackBeltNinja
March 24th, 2005, 05:07 PM
I copyied the script and
this is the error i get from, jerry...
[Thu Mar 24 16:06:14 2005] [error] PHP Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /WebRoot/eblast/upload.php on line 4
it's just a white page, nothing shows up in the browser
BlackBeltNinja
March 24th, 2005, 05:17 PM
and the same error comes up if i change $_FILES to $HTTP_POST_FILES
Jerryscript
March 25th, 2005, 03:19 AM
What version of PHP are you running?
I just cc&p'd the script to my server, and it uploaded an image to my website fine. I'm running PHP4.3.3 on Apache1.3.28 (linux) online, and PHP5.1.0-dev on Apache 1.3.29 (windows) locally.
amitgeorge
March 25th, 2005, 03:43 AM
try usind some open source php application like phpMyadmin. they have tested upload scripts. if that does not work, means your server has some problem, if it does try to take code from there
BlackBeltNinja
March 25th, 2005, 11:45 AM
i'm using php 4.3.10 on apache (i dunno what version of that). you can see the error message at http://www.cityofninjas.com/upload.php
if you msg me on aim at metalslugmaster maybe i could let you take a look for yourself.
hl
March 25th, 2005, 12:02 PM
your using mysql right ... http://www.spoono.com/php/tutorials/tutorial.php?id=42
BlackBeltNinja
March 25th, 2005, 05:14 PM
i've seen this tutorial already, but my problem is that i need to upload to the server before i can place the images into mysql
hl
March 26th, 2005, 07:52 PM
ah... do you mean you want some sort of webftp...?
otherwise i don't get your previous comment.
RomantorV
March 27th, 2005, 09:10 PM
function checkFile($userfile, $maxsize)
{
$mimetypes = array(
"image/x-png" => 1,
"video/vivo" => 1,
"video/x-msvideo" => 1,
"audio/x-pn-realaudio" => 1,
"image/gif" => 1,
"video/mpeg" => 1,
"image/ico" => 1,
"image/x-MS-bmp" => 1,
"image/tiff" => 1,
"video/quicktime" => 1,
"image/jpeg" => 1,
"image/pjpeg" => 1,
// new extension
"application/pdf" => 1
);
if ((empty($userfile['name'])) || ($userfile['name'] == "none"))
return false;
if (($userfile['size'] == 0) || ($userfile['size'] > $maxsize))
return false;
//if (!isset($mimetypes[$userfile['type']]))
//return false;
return true;
}
function uploadFile($userfile, $maxsize, $upload_dir)
{
if (!checkFile($userfile, $maxsize))
return false;
$tmp_dir = $userfile['tmp_name'];
$name = $upload_dir."/".$userfile['name'];
if (!@move_uploaded_file($tmp_dir, $name))
return false;
return true;
}
#use it as below
uploadFile($HTTP_POST_FILES['image'], 1024*1000, "uploads")
/*
You must creat a dir name uploads at the same level of the file contain this code.
And your form must be "multipart/form-data" and have the file field name "image"
*/
Good luck!
BlackBeltNinja
March 28th, 2005, 11:51 AM
what is the image of $HTTP_POST_FILES['image']?
is that the name of the form holding the file?
BlackBeltNinja
March 28th, 2005, 12:00 PM
I GOT IT!!! thanks guys! :)
here's what i did.
UPLOAD.HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form enctype='multipart/form-data' method='post' action='upload.php'>
<input type='hidden' name='MAX_FILE_SIZE' value='30000' />
Send this file: <input name='userfile' type='file' />
<input type='submit' value='Send File' />
</form>
</body>
</html>
UPLOAD.PHP
<?
//upload.php
function checkFile($userfile, $maxsize)
{
$mimetypes = array(
"image/x-png" => 1,
"video/vivo" => 1,
"video/x-msvideo" => 1,
"audio/x-pn-realaudio" => 1,
"image/gif" => 1,
"video/mpeg" => 1,
"image/ico" => 1,
"image/x-MS-bmp" => 1,
"image/tiff" => 1,
"video/quicktime" => 1,
"image/jpeg" => 1,
"image/pjpeg" => 1,
// new extension
"application/pdf" => 1
);
if ((empty($userfile['name'])) || ($userfile['name'] == "none"))
return false;
if (($userfile['size'] == 0) || ($userfile['size'] > $maxsize))
return false;
//if (!isset($mimetypes[$userfile['type']]))
//return false;
return true;
}
function uploadFile($userfile, $maxsize, $upload_dir)
{
if (!checkFile($userfile, $maxsize))
return false;
$tmp_dir = $userfile['tmp_name'];
$name = $upload_dir."/".$userfile['name'];
if (!@move_uploaded_file($tmp_dir, $name))
return false;
return true;
}
#use it as below
uploadFile($_FILES['userfile'], 1024*1000, "uploads")
/*
You must creat a dir name uploads at the same level of the file contain this code.
And your form must be "multipart/form-data" and have the file field name "image"
*/
?>
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.