Jump to content

[SOLVED] File Upload - File not moving


twilitegxa

Recommended Posts

In the following script, the file APPEARS to be moved successfully (the message displays saying it has been moved successfully), but when I check the destination folder, the file has not been copied. Here is the form and script to move the file:

 

Form:

<html>
<head>
<title>A Simple File Upload Form</title>
</head>
<body>
<form action="listing9.14.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="max_file_size" value="51200" />
File To Upload:
<input type="file" name="fileupload" /><br /><br />
<input type="submit" value="Upload!" />
</form>
</body>
</html>

 

Upload:

<html>
<head>
<title>A File Upload Script</title>
</head>
<body>
<h1>File Upload Results</h1>
<?php
$file_dir = "/images";

foreach($_FILES as $file_name => $file_array) {
print "path: ".$file_array['tmp_name']."<br>\n";
print "name: ".$file_array['name']."<br>\n";
print "type: ".$file_array['type']."<br>\n";
print "size: ".$file_array['size']."<br>\n";

if (is_uploaded_file($file_array['tmp_name'])) {
	move_uploaded_file($file_array['tmp_name'],
		"$file_dir/$file_array[name]") or die ("Couldn't copy");
	print "file was moved!<br><br>";
}
}
?>
</body>
</html>

 

What am I doing wrong?

Link to comment
Share on other sites

its 5:58 AM and i'm really tired lol...

i didnt look at your code... but i made one a few weeks ago for my gallery

so you can use it, i dont realy care....

 

<?php
//+---------------------------------------------------------------------------------
//
// ADD IMAGE
//
//+---------------------------------------------------------------------------------

function add_img() {
	global $DB, $cms, $std;

	$DB->query("SELECT * FROM cms_albums");

	while ($a = $DB->fetch_row())
	{
		$album_list .= '<option value="'.$a['id'].'">'.$a['name'].'</option>';
	}

	//+---------------------------------------------------
	// Start printing our HTML
	//+---------------------------------------------------

	$html .= '<form ENCTYPE="multipart/form-data" action="'.$this->url.'&code=do_add_img" method="post">';
	$html .= '<input type="file" name="image" size="35">';
	$html .= '</form>';

	return $html;
}	

//----------------------------------------------------------------------------------------------

function do_add_img() {
	global $DB, $cms, $std;

	// Make some changes in the file name
	$random = rand(0,1000000000000000000);
	$_FILES["image"]["name"] = "".$random."_".$_FILES["image"]["name"]."";

	/*-------------------------------------------------------------------------*/
	// Make some checks
	/*-------------------------------------------------------------------------*/

	$type = $_FILES["image"]["type"];

	$at = array("image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/x-png");

	// We allow only images file types
	if (! in_array($type, $at) )
	{
		return $std->error("אתה מנסה לעלות תמונה שהיא לא בפורמט תקין.");
	}

	// If we got some errors in uploading, display it
	if ($_FILES["image"]["error"] > 0)
	{
		return $std->error("{$_FILES['image']['error']}");
	}

	// If the file name alread exist, dispaly an error
	if (file_exists("".$this->dir."/" . $_FILES["image"]["name"]))
	{
		return $std->error("שם הקובץ כבר קיים בשרת, החלף את השם בשם חדש.");
	}

	/*-------------------------------------------------------------------------*/
	// If we got here with no error, so just store the file
	/*-------------------------------------------------------------------------*/

	move_uploaded_file($_FILES["image"]["tmp_name"], "".$this->dir."/" . $_FILES["image"]["name"]);

}
?>	

 

there are some parts in hebrew but its only a error that might be displayed...

 

it's works great

Link to comment
Share on other sites

I really appreciate the code, but I'm still pretty new to PHP and My SQL, so I'm trying to learn from a book to get started. In this tutorial, it doesn't seem to save the files uploaded to the database. Isn't that necessary? I'm not sure how this works, but your sample code seems like that's what it does as well.

Link to comment
Share on other sites

Your second argument for move_uploaded_file() looks weird.  Try

   if (is_uploaded_file($file_array['tmp_name'])) {
      move_uploaded_file($file_array['tmp_name'],
         $file_dir . "/" . $file_array['name']) or die ("Couldn't copy");
      print "file was moved!<br><br>";
   }

Though that may not change anything

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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