File uploading - need help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

File uploading - need help

Post by vigge89 »

Im creating a upload page for my site, but right now it doesn't work, the code looks like this:

Code: Select all

<div class="c2box">
<table class="txt" width="480" cellspacing="0" cellpadding="0">

<?php

if(!isset($upload)) {
$upload = "";
}

switch($upload) {
default:
include("source/uplcfg.php");

echo "
<tr><td class="tblheader"><font class="txtheader">Uploader</font></td></tr>
<tr><td>Maximum filesize is 500kb (0,5mb).<br>Filetypes allowed are: jpg, png, gif, bmp, psd, zip and txt.</td></tr>
<form ENCTYPE="multipart/form-data" method="post" action="index.php?id=uploader&upload=upload">
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<tr><td>Filename: <input type="file" name="file" SIZE="25"></td></tr>
<tr><td><input type="submit" value=" upload "></td></tr>
</form>
<tr><td><br><a href="index.php?id=uploader&op=view">View uploaded files</a></td></tr>
";

break;
case "upload":
include "source/uplcfg.php";
$result = "Uploaded file succesfully!<br>File location: <a href="$full_path/$file_name">$full_path/$file_name</a>";
if ($file_name == "") {
$result = "No file selected!";
}else{
if(file_exists("$upl_path/$file_name")) {
$result = "File does already exist, try renaming the file to upload.";
} else {
if (($size_limit == "yes") && ($limit_size < $file_size)) {
$result = "The file was to big, maximum filesize allowed is 500kb.";
} else {
$ext = strrchr($file_name,'.');
if (($limit_ext == "yes") && (!in_array($ext,$extensions))) {
$result = "The type of the file you are trying to upload isn't allowed.";
}else{
@copy($file, "$upl_path/$file_name") or $result = "The file could not be copied to the server."; // This is the error i get (PHP says that the file or directory doesn't exist)
}
}
}
}

echo "
<tr><td class="tblheader"><font class="txtheader">Uploader</font></td></tr>
<tr><td>$result</td></tr>
<tr><td><br><a href="index.php?id=uploader">Back</a><br><a href="index.php?id=uploader&op=view">View uploaded files</a></td></tr>
";

break;
}
?>

</table>
</div>
But when i run it, i get the error saying "The file could not be copied to the server.".
Im running the script in the root (htdocs) on my apache server, and the folder the files should be uploaded to is named "Uploaded".
I think ive missed something here :/
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

Contents of uplcfg.php:

Code: Select all

<?php
$upl_path = "uploaded/"; //path to where files are uploaded (from root)
$full_path = "http://armyhosting.net/vigge/uploaded"; //Full path (www) where the fles will be uploaded
$size_limit = "yes";
$limit_size = "500000";
$limit_ext = "yes";
$ext_count = "7";
$extensions = array(".gif", ".jpg", ".jpeg", ".png",".psd",".zip",".txt");
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Is the "uploaded/" folder already created? PHP cannot upload to a folder that doesn't exist. I forget that all the time and spend hours trying to figure out what I did wrong. Also, to get the "uploaded/" folder from the root you need to do something like:

Code: Select all

<?php
$upl_path = $_SERVER['DOCUMENT_ROOT'] . "/uploaded/"; //path to where files are uploaded (from root) 
?>
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

DuFF wrote:Is the "uploaded/" folder already created? PHP cannot upload to a folder that doesn't exist. I forget that all the time and spend hours trying to figure out what I did wrong. Also, to get the "uploaded/" folder from the root you need to do something like:

Code: Select all

<?php
$upl_path = $_SERVER['DOCUMENT_ROOT'] . "/uploaded/"; //path to where files are uploaded (from root) 
?>
yes, it's already created.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

try inserting this into your code in the sections that processes the uploaded file and post back what info it returns.

Code: Select all

print_r($_FILES);

Mark
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

Mark:
output of it is:
Array ( [file] => Array ( [name] => picture.jpg [type] => image/jpeg [tmp_name] => C:\WINDOWS\TEMP\php4193.TMP [error] => 0 [size] => 10807 ) )
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

one thing i didn notice is this line

Code: Select all

<tr><td><input type="submit" value=" upload "></td></tr>
the value of submit will be " upload " (notice the spaces)

whereas you are checking for "upload"

This will return false!

try removing the spaces from the name value of the submit button

Mark
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

ok, ill test it out now, i thought the value of submit was just for the button text?
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

hmmm, still doesn't work :(
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

ooops yeah,

sorry my bad
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you tried some simple test code along the lines of that supplied here:
http://www.php.net/manual/en/features.file-upload.php

Mac
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

well, this script works, but not the other one;

Code: Select all

<?php
if(!isset($upload)) {
$upload = "";
}
switch($upload) {
default:
include "source/uplcfg.php";
echo "
<html>

<head>
<title>Upload</title>
</head>

<body>

<form method="POST" action="upload_w.php?upload=doupload" enctype="multipart/form-data">
<input type="file" name="file" size="30"><br>
<br>
<button name="submit" type="submit">Upload</button>
</form>

</body>

</html>";
break;
case "doupload":
include "source/uplcfg.php";
$result = "Uploaded file succesfully!<br>File location: <a href="$full_path$file_name">$full_path$file_name</a>";
if ($file_name == "") {
$result = "No file selected!";
}else{
if(file_exists("$upl_path/$file_name")) {
$result = "File does already exist, try renaming the file to upload.";
} else {
if (($size_limit == "yes") && ($limit_size < $file_size)) {
$result = "The file was to big, maximum filesize allowed is 500kb.";
} else {
$ext = strrchr($file_name,'.');
if (($limit_ext == "yes") && (!in_array($ext,$extensions))) {
$result = "The type of the file you are trying to upload isn't allowed.";
}else{
@copy($file, "$upl_path$file_name") or $result = "The file could not be copied to the server.";
}
}
}
}
copy($file, "$upl_path$file_name");
echo "
<html>

<head>
<title>Upload</title>
</head>

<body>
$result
</body>

</html>";
break;
}
?>
I think ive forgotten something :/
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

Ok, edited the whole site, and then i figured out what was the problem, when i included the file in my index.php it wouldn't work, but when i ran the page itselft, it worked perfectly. Something on my index.php makes the code not to work.

Code for index.php:

Code: Select all

<?php
//Include variables
include('pgvars.php');
?>

<HTML>
<HEAD>

<link rel="stylesheet" href="cube.css">
<title>The Cube<?php echo $pagetitle ?></title>

</HEAD>

<BODY bgcolor="#606060" background="img/bg.gif">
<center>

<table border="0" cellspacing="0" cellpadding="5" width="624" align="center">

<tr><td valign="top" align="right" width="110">
<!-- MENU starts -->

<?php include ("menu.php") ?>

<!-- MENU ends -->
</td>

<td valign="top" align="center" width="510">


<div class="conbox">
<table cellspacing="0" width="500" cellpadding="0">
<tr>
	<td class="tblheader"><font class="pagetitle"><?php echo $header ?></font></td>
</tr>
<tr>
	<td width="tblheader2"><font class="txtheader"><center><?php echo $underheader ?></center></font></td>
</tr>

</table>
</div>

<br>

<div class="conbox"><center>
<br>

<?php
//Include content
	$print_what = "content";

	//Print it
	$ext = ".php";
	if (isset($_GET['id'])) {
		$id = "".addslashes($_GET['id'])."".$ext."";
		if (file_exists($id)) {
			include("$id");
		}
		else {
			include("404.php");
		}
	}
	else {
		include("start.php");
	}
?>

<br>
</div>

</td></tr>
</table>
</td></tr>
</table>
</center>

</BODY>
</HTML>
But, as i have also included other pages, they could also lead to the problem, ill post them here later, if i don't fins the problem
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

solved, i tested with an iframe and it worked...
Post Reply