uploading pictures

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
azqnow
Forum Newbie
Posts: 1
Joined: Tue Jun 22, 2004 9:31 pm

uploading pictures

Post by azqnow »

need help in uploading files heres the code.. the problem is file size.. im a newbie i dont know how to upload files with varying sizes and file types.. i want to upload different kind of files but this one is for pictures only specifically jpeg

//========= uploadpic.php ==============

Code: Select all

<?php
$city = $_POST['city'];
$i = $_POST['i'];
$city = trim($city);
$userfile = $_FILES['userfile']['tmp_name'];
$userfile_name = $_FILES['userfile']['name'];
$userfile_size = $_FILES['userfile']['size'];
$userfile_type = $_FILES['userfile']['type'];
if ($userfile == "none") {
	echo "<html>";
	echo "<head>";
	       echo "<title>Port Information System: Picture Upload For $city</title>";
		echo "<link rel="stylesheet" type="text/css" href="display.css" />";
	echo "<META HTTP-EQUIV=Refresh CONTENT="2; URL=".getenv("HTTP_REFERER")."">";
	echo "</head>";
	echo "<body>";
	exit("<center><br><br><br><br><br><br><br><br><br><br><h3>Problem: no file uploaded</h3></center>");
}
if ($userfile_size == 0) {
	echo "<html>";
	echo "<head>";
	       echo "<title>Port Information System: Picture Upload For $city</title>";
		echo "<link rel="stylesheet" type="text/css" href="display.css" />";
	echo "<META HTTP-EQUIV=Refresh CONTENT="2; URL=".getenv("HTTP_REFERER")."">";
	echo "</head>";
	echo "<body>";
	exit("<center><br><br><br><br><br><br><br><br><br><br><h3>Problem: uploaded file is zero length</h3></center>");
}
if ($userfile_size > 800000000) {
	echo "<html>";
	echo "<head>";
	       echo "<title>Port Information System: Picture Upload For $city</title>";
		echo "<link rel="stylesheet" type="text/css" href="display.css" />";
	echo "<META HTTP-EQUIV=Refresh CONTENT="2; URL=".getenv("HTTP_REFERER")."">";
	echo "</head>";
	echo "<body>";
	exit("<center><br><br><br><br><br><br><br><br><br><br><h3>Problem: uploaded file is greater than 8mb</h3></center>");
}
if ($userfile_type == "text/plain") {
	echo "<html>";
	echo "<head>";
	       echo "<title>Port Information System: Picture Upload For $city</title>";
		echo "<link rel="stylesheet" type="text/css" href="display.css" />";
	echo "<META HTTP-EQUIV=Refresh CONTENT="2; URL=".getenv("HTTP_REFERER")."">";
	echo "</head>";
	echo "<body>";
	exit("<center><br><br><br><br><br><br><br><br><br><br><h3>Problem: file is not image</h3></center>");
}

if (!is_uploaded_file($userfile)) {
	echo "<html>";
	echo "<head>";
	       echo "<title>Port Information System: Picture Upload For $city</title>";
		echo "<link rel="stylesheet" type="text/css" href="display.css" />";
	echo "<META HTTP-EQUIV=Refresh CONTENT="2; URL=".getenv("HTTP_REFERER")."">";
	echo "</head>";
	echo "<body>";
	exit("<center><br><br><br><br><br><br><br><br><br><br><h3>Problem: possible file upload attack</h3></center>");
}
header("Location: ".getenv("HTTP_REFERER"));
$upfile = "../litrato/".$userfile_name;
$upfile2 = "../litrato/".$i.$city.".jpg";
rename($upfile, $upfile2);
if (!copy($userfile, $upfile2)){
	echo "<center><br><br><br><br><br><br><br><br><br><br><h3>Problem: Could not move file into directory</h3></center>";
	exit;
}
//echo "File uploaded successfully<br><br>";
?>
</body>
</html>
// end of uploadpic.php ===============================


feyd | use

Code: Select all

tags when posting code; Read [/color][url=http://forums.devnetwork.net/viewtopic.php?t=21171] [color=red][u][b]Posting Code in the Forums[/b][/u][/color][/url].
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

times like these i wish i was a mod!

1)Post this type of question the Programming Section under PHP-Code
2)use tags when posting php code
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Read the section of the manual regarding uploading via HTTP. There's a form element that can set the max uploaded file size.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply