Click to See Complete Forum and Search --> : uploader help
metrosoccer12
07-31-2003, 03:55 PM
I'm new to php and i tryed installing this code can anyone tell me if i need to change anything or why its not working?
file name: upload.php
Code:
<html>
<head>
<title>Upload a File</title>
</head>
<body>
<?PHP
$uploaddir = "uploads/"; # must be chmoded to 777
if ($_POST['submit'])
{
$name = $_FILES['File']['name'];
$tmpname = $_FILES['File']['tmp_name'];
$size = $_FILES['File']['size'];
if (move_uploaded_file($tmpname, $uploaddir . $name)) {
print ("File Name: $name<br/><br/>\n");
print ("File Size: $size<br/><br/>\n");
print ("Your file was successfully uploaded!<br/><br/>\n");
}
else
{
print ("Your file could not be uploaded.");
}
}
?>
Upload a file to the server
<form action="upload.php" method="post" enctype="multipart/form-data">
File: <input type="file" name="File" size="20"><br/>
<input type="submit" name="submit" value="Upload File"></form>
</body>
</html>
What version of PHP are you running?
metrosoccer12
07-31-2003, 04:01 PM
i have no idea... i will ask my host but is there a code for an older version i can use for now?
You can check yourself with a phpinfo() script:
<?PHP
phpinfo ();
?>No need to use anything else, if that isn't the problem, so let me know what version of PHP you have (or better yet, post a link to the phpinfo() script.
metrosoccer12
07-31-2003, 04:07 PM
http://www.krylonblue.net/easiertorun/upload.php
PHP Version 4.3.2
Try switching move_uploaded_file to copy
Also, did you CHMOD the directory that you are trying to upload to?
metrosoccer12
07-31-2003, 04:15 PM
ooo i chmod the directory oo i thought i chmod the file...
metrosoccer12
07-31-2003, 05:13 PM
okay i got it and i understand it now thx!
but i have a few more questions... can i make it so only gif and jpg files can be uploaded? and is there a code i can use to show all the files in a certin dicrtiory in this case uploads
#1 can be achived with something like this:
$name = $_FILES['File']['name'];
$tmpname = $_FILES['File']['tmp_name'];
$size = $_FILES['File']['size'];
$name = split("\.","$name");
$ext = $name[count($name)-1];
if ($ext == "gif" || $ext == "jpg" || $ext == "jpeg" || $ext == "jpe") {
#upload code goes here
}
else {
echo "Only GIF and JPEG files are allowed.";
}
For #2, you'll need something along these lines:
<?PHP
$folder = "/root/path/to/uploads/"; #set the path to the uploads directory
$handle = opendir($folder);
# Making an array containing the files in the current directory:
while ($file = readdir($handle))
{
$filename[] = $file;
}
closedir($handle);
# Sorting the files with newest files at the top
sort ($filename);
for ($i = 0; $i < sizeof($filename); $i++) {
if ($filename[$i] == "." || $filename[$i] == "..") {
}
else {
echo "<a href=\"$filename[$i]\">$filename[$i]</a><br>\n";
}
}
?>
metrosoccer12
07-31-2003, 05:29 PM
okay were do i add the first code? i know where to add the second code but i dont have a clue on the first lol
Here's the first one, with it all added together:
<?PHP
$uploaddir = "uploads/"; # must be chmoded to 777
if ($_POST['submit']) {
$name = $_FILES['File']['name'];
$tmpname = $_FILES['File']['tmp_name'];
$size = $_FILES['File']['size'];
$name = split("\.","$name");
$ext = $name[count($name)-1];
if ($ext == "gif" || $ext == "jpg" || $ext == "jpeg" || $ext == "jpe") {
if (move_uploaded_file($tmpname, $uploaddir . $name)) {
echo "File Name: $name<br/><br/>\n";
echo "File Size: $size<br/><br/>\n";
echo "Your file was successfully uploaded!<br/><br/>\n";
}
else {
echo "Your file could not be uploaded.";
}
}
else {
echo "Only GIF and JPEG files are allowed.";
}
}
?>
metrosoccer12
07-31-2003, 05:35 PM
o and sorry if im a pain but how do i make a link like:
<a href="IMAGENAME"><img src="IMAGENAME" border="0">
i dont know how to do that to the second code.......
thanks in advance
You lost me on that one... what do you need to link to?
metrosoccer12
07-31-2003, 05:44 PM
let me explain:
i kind of want an image gallery were people can just upload files into it, so i want to make it gif and jpg only, and i dont know where to put the code u gave me.
Okay u know how on the other code u made it like link to the file? well can u do it so its shows the image instead of linking to it?
Also it links to my main site not the /uploads folder the code u gave me it comes out like this:
http://www.krylonblue.net/easiertorun/IMAGEURL.gif
but i want it like this
http://www.krylonblue.net/easiertorun/uploads/IMAGEURL.gif
switch this part:
else {
echo "<a href=\"$filename[$i]\">$filename[$i]</a><br>\n";
}
to:
else {
echo "<img src=\"$filename[$i]\"><br>\n";
}
metrosoccer12
07-31-2003, 05:54 PM
oo the code u gave me only allowing gifs and jpgs wont work i cant upload any files
neenach2002
07-31-2003, 06:27 PM
ooo....cool...a Linkin' Park site...I'll be all over that one!
I only know cuz I deleted "upload.php" from the end of the URL...
here is how to make an image link
<a href="name of URL or PICTURE"><img src="LOCATION of PICTURE"></a>
there you go!:D :D :D
I haven't even gotten a book on PHP yet so I can't help you there:D
metrosoccer12
07-31-2003, 08:33 PM
well that wont work in php anyway...... trash the idea lol cause i cant moderate the uploads 24/7
i may use it later....
neenach2002
07-31-2003, 08:46 PM
actually...I goofed...AGAIN!
it isn't
<a href="name of URL or PICTURE"><img src="LOCATION of PICTURE"></a>
it's
<a href="URL where you want person to go"><img src="LOCATION of PICTURE"></a>
sorry!:D :D :D
metrosoccer12
07-31-2003, 10:35 PM
well its different in php so that wont work :) i already knew that code