Jump to content

failed to open stream?


dftg

Recommended Posts

I'm trying to use this code:

<?php  // actual code for upload
$folder_name = date("mdy");
if($_FILES["zip_file"]["name"]) {
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$type = $_FILES["zip_file"]["type"];

$name = explode(".", $filename);
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
foreach($accepted_types as $mime_type) {
	if($mime_type == $type) {
		$okay = true;
		break;
	} 
}

$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue) {
	$message = "The file you are trying to upload is not a .zip file. Please try again.";
}

$target_path = "/$folder_name/";  // change this to the correct site path
if(move_uploaded_file($source, $target_path)) {
	$zip = new ZipArchive();
	$x = $zip->open($target_path);
	if ($x === true) {
		$zip->extractTo("/".$folder_name."/"); // change this to the correct site path
		$zip->close();

		unlink($target_path);
	}
	$message = "Your .zip file was uploaded and unpacked.";
} else {	
	$message = "There was a problem with the upload. Please try again.";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php if($message) echo "<p>$message</p>"; ?>
<form enctype="multipart/form-data" method="post" action=""> 
// computer browse form

<label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
<br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>

 

to upload a zip file to a directory. I'm getting this error message:

Warning: move_uploaded_file(/081112/) [function.move-uploaded-file]: failed to open stream: Is a directory in /home/content/G/r/a/Gravid/html/headfirst/test.php on line 23

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpuHkilB' to '/081112/' in /home/content/G/r/a/Gravid/html/headfirst/test.php on line 23

There was a problem with the upload. Please try again.

 

I've tried adjusting the folder to which I'm uploading, and correcting the pathname in the $target_path variable, but I can't seem to get around this. What am I doing wrong here?

Link to comment
Share on other sites

Ok, I tried changing the line in question to simply

 

$target_path = $folder_name;

 

which results in pretty much the same error message:

 

Warning: move_uploaded_file(081212) [function.move-uploaded-file]: failed to open stream: Is a directory in /home/content/G/r/a/Gravid/html/headfirst/test.php on line 22

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpQcbjrS' to '081212' in /home/content/G/r/a/Gravid/html/headfirst/test.php on line 22

There was a problem with the upload. Please try again.

 

And still the zip file is not in the target folder.

 

I also tried

 

$target_path = "/home/content/G/r/a/Gravid/html/headfirst/".$folder_name;

 

and got the same result.

 

Thank you for helping me.

Link to comment
Share on other sites

  • 2 weeks later...

OK so, moving a little bit further forward with this, I've got a couple issues I can't figure out.

 

1. I have the foreach loop printing out the hard url of each of the files that's been uploaded. However, it also prints out the urls of two extra files, the dot and double dot for the root directory...how do I make those go away from the foreach echo?

 

2. I'm trying to have the foreach loop load some info into a mysql table for each of the files. No matter how many files I attempt to upload, I'm ending up with one single row of data getting saved to the table. Why is this happening? I should be getting one row for each file in the zip file.

 

3. One more tiny little thing - I'm having trouble formatting that hard url. I end up with spaces between where it should be concatenating.

 

<?php  // actual code for upload
$dirname = $_REQUEST['dirname'];
$taken = $_REQUEST['taken'];
$location = $_REQUEST['location'];
$subject = $_REQUEST['subject'];
$urldirectory = $_REQUEST['urldirectory'];

if(!(file_exists($dirname) && is_dir($dirname))) {
mkdir($dirname, 0777);
echo "the directory will be called ".$dirname;
} else {
echo "directory: " . $dirname;
}

if($_FILES["zip_file"]["name"]) {
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$type = $_FILES["zip_file"]["type"];

$name = explode(".", $filename);
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
foreach($accepted_types as $mime_type) {
	if($mime_type == $type) {
		$okay = true;
		break;
	} 
}

$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue) {
	$message = "The file you are trying to upload is not a .zip file. Please try again.";
}
	$target_path = $dirname."/".$name;  	

if(move_uploaded_file($source, $target_path)) {

	$zip = new ZipArchive();
	$x = $zip->open($target_path);
	if ($x === true) {
		$zip->extractTo($dirname."/"); 
		$zip->close();
		unlink($target_path);
	}

		$message = "Your .zip file was uploaded and unpacked.";
		$images = scandir($dirname); //use scandir to find all the files that have been unzipped into the directory, and then do a foreach loop that enters the image file locations into your mysql database
		require 'connect.php';
		echo '<html>'; 
		foreach ($images as $value) {
			$url = $urldirectory."/".$value;
			echo $url;
			$insert_sql = "INSERT INTO pics (taken, location, subject, url) " . "VALUES ('{$taken}', '{$location}', '{$subject}' ,'{$url}');";
			mysql_query($insert_sql);
			//echo '<img src="././headfirst/' . $dirname . '/' . $value . '"< /img>';
			}
				} else {	
		$message = "There was a problem with the upload. Please try again.";}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php 
if($message) echo "<p>$message</p>";
if($taken) echo "<p>pictures taken on: " . $taken . "</p>";
if($subject) echo "<p>subject: " . $subject  . "</p>";
if($location) echo "<p>location: " . $location . "</p>";
?>
<form enctype="multipart/form-data" method="post" action="test.php"> 
<label for="dirname">Directory to use?: </label> <input name="dirname" size="20" type="text" value="<?php echo $dirname; ?>" /><br />
<label for="taken">When was this taken?:</label> <input name="taken" size="20" type="text" value="<?php echo $dirname; ?>" /><br />
<label for="location">Where was this taken?</label> <input name="location" size="20" type="text" /><br />
<label for="subject">subject?</label> <input name="subject" size="20" type="text" /><br />
<input type=hidden name="urldirectory" value="<?php echo '././'.$dirname; ?> " />
<label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
<br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>

Link to comment
Share on other sites

1. Use glob () instead of scandir ().

2. You're not checking for errors, so this can be caused by any number of reasons. Most likely a problem with the SQL query or table structure.

3. Could you post an example of these added spaces, and point out what line you're getting them from?

Link to comment
Share on other sites

  • 3 weeks later...

Ok, my code as of now.

 

Scandir() seems to be returning a better result than glob, so I'm sticking with that. I'd have to make some more changes to make glob() work, but that's what I'll try next.

 

I added error handling to the code. The result now is that the mysql_insert() will insert the first result and nothing beyond the first result. I'll also get an error message if it doesn't work at all. That's a bit of progress, at least. I'm still trying to figure this out, I'll keep at it.

 

I am still getting that space between the variables that are printed out by line #54 - this is the line: $url = trim($urldirectory)."/".trim($value); . I thought that using trim() might get rid of that space between the variables, but it has not.

 

 

 

 

<?php  // actual code for upload
$dirname = $_REQUEST['dirname'];
$taken = $_REQUEST['taken'];
$location = $_REQUEST['location'];
$subject = $_REQUEST['subject'];
$urldirectory = $_REQUEST['urldirectory'];

if(!(file_exists($dirname) && is_dir($dirname))) { // confirm that the directory exists, and that it is a directory
mkdir($dirname, 0777);
echo "the directory will be called ".$dirname;
} else {
echo "directory: " . $dirname;
}

if($_FILES["zip_file"]["name"]) { // pull the nmae of the zip file from the upload
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$type = $_FILES["zip_file"]["type"];

$name = explode(".", $filename); //format the filename for a variable
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
foreach($accepted_types as $mime_type) {
	if($mime_type == $type) {
		$okay = true;
		break;
	} 
}

$continue = strtolower($name[1]) == 'zip' ? true : false; // let user know if the zip file has not been uploaded
if(!$continue) {
	$message = "The file you are trying to upload is not a .zip file. Please try again.";
}
	$target_path = $dirname."/".$name; // get the $target_path variable to for the move_uploaded_file() function.

if(move_uploaded_file($source, $target_path)) { // this block extracts the zip files and moves them to the $dirname directory

	$zip = new ZipArchive();
	$x = $zip->open($target_path);
	if ($x === true) {
		$zip->extractTo($dirname."/"); 
		$zip->close();
		unlink($target_path);
	}

		$message = "Your .zip file was uploaded and unpacked.";
		//use scandir to find all the files that have been unzipped into the directory, and then do a foreach loop that enters the image file locations into your mysql database
		require_once 'connect.php';
		echo '<html>'; 

	$images = scandir($dirname); //use scandir to find all the files that have been unzipped into the directory, and then do a foreach loop that enters the image file locations into your mysql database
		echo "Number of pics uploaded: " . count(array($images));
		echo '<html>'; 
		foreach ($images as $value) {
			$url = trim($urldirectory)."/".trim($value);
			echo '<img src="http://example.com/' . $dirname . '/' . $value . '"< /img>';
					}
			echo '</html>';
			$insert_sql = "INSERT INTO pics (taken, location, subject, url) VALUES ('$taken', '$location', '$subject' , '$url');";
				if (mysql_query($insert_sql)) {	
						echo "$value"." inserted successfully!";
					} else {	
						echo "$value"." not inserted";
					}
				} else {	
		$message = "There was a problem with the upload. Please try again.";
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php 
if($message) echo "<p>$message</p>";
if($taken) echo "<p>pictures taken on: " . $taken . "</p>";
if($subject) echo "<p>subject: " . $subject  . "</p>";
if($location) echo "<p>location: " . $location . "</p>";
?>
<form enctype="multipart/form-data" method="post" action="test.php"> 
<label for="dirname">Directory to use?: </label> <input name="dirname" size="20" type="text" value="<?php echo $dirname; ?>" /><br />
<label for="taken">When was this taken?:</label> <input name="taken" size="20" type="text" value="<?php echo $dirname; ?>" /><br />
<label for="location">Where was this taken?</label> <input name="location" size="20" type="text" /><br />
<label for="subject">subject?</label> <input name="subject" size="20" type="text" /><br />
<input type=hidden name="urldirectory" value="<?php echo'http://gravenimage.us/headfirst/'.$dirname;?>" />
<label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
<br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>

Link to comment
Share on other sites

At around line 62, when the insert fails, add the line below. Then post the SQL and the error message. There is either an error in the INSERT statement, or a duplicate key issue (or something else).

 

echo "$value"." not inserted";  # This is line 62
echo $insert_sql . '<BR>' . mysql_error();

 

Nevermind Your INSERT is OUTSIDE of the foreach loop, so it is only being called once. You need to work on your code formatting so you can see where loops and things close. Here is that segment of code reformatted so you can see where the loop ends:

		$images = scandir($dirname); //use scandir to find all the files that have been unzipped into the directory, and then do a foreach loop that enters the image file locations into your mysql database
	echo "Number of pics uploaded: " . count(array($images));
	echo '<html>'; 
	foreach ($images as $value) {
		$url = trim($urldirectory)."/".trim($value);
		echo '<img src="http://example.com/' . $dirname . '/' . $value . '"< /img>';
	} # END OF FOREACH LOOP
	echo '</html>';
	$insert_sql = "INSERT INTO pics (taken, location, subject, url) VALUES ('$taken', '$location', '$subject' , '$url');";
	if (mysql_query($insert_sql)) {	
		echo "$value"." inserted successfully!";
	} else {	
		echo "$value"." not inserted";
	}

 

You could move the insert into the loop. Of course just saying that will start a small brush fire. The preferred (and more advanced) way is to collect the values you want to insert and do one insert after the loop:

		$images = scandir($dirname); //use scandir to find all the files that have been unzipped into the directory, and then do a foreach loop that enters the image file locations into your mysql database
	echo "Number of pics uploaded: " . count(array($images));
	echo '<html>'; 
	$dbValues = array();
	foreach ($images as $value) {
		$url = trim($urldirectory)."/".trim($value);
		echo '<img src="http://example.com/' . $dirname . '/' . $value . '"< /img>';
		$dbValues[] = sprintf("('%s', '%s', '%s', '%s')", mysql_real_escape_string($taken),
				mysql_real_escape_string($location), mysql_real_escape_string($subject),
				mysql_real_escape_string($url));
	}
	echo '</html>';
	$insert_sql = "INSERT INTO pics (taken, location, subject, url) VALUES " . implode(',',$dbValues);
	if (mysql_query($insert_sql)) {	
		echo "pics inserted successfully!";
	} else {	
		echo "pics not inserted";
	}

 

Note: You are not escaping the values you are putting into the query. This could break the query, or worse, allow for SQL injection attacks. You should use mysql_real_escape_string.

 

$insert_sql = "INSERT INTO pics (taken, location, subject, url) VALUES ('" . 
  mysql_real_escape_string($taken) . "', '" . 
  mysql_real_escape_string($location) . "', '" . 
  mysql_real_escape_string($subject) . "' , '" . 
  mysql_real_escape_string($url) . "');";

 

As to the spaces in the URL. You need to echo out the value and post it in a code block so we can see what it is and possibly where it comes from. Line 54 should definitely NOT be leaving spaces between those two elements that you are concatenating.

Link to comment
Share on other sites

  • 3 weeks later...

Alright, here's where I'm at now.

 

DavidAM:

  1. thank you for your advice.
     
  2. I followed your advice on the adjusting where the end of the foreach loop fell, and that worked.
     
  3. I'll be working mysql_real_escape_string() in a.s.a.p.
     
  4. I solved the concatenation issue by adjusting that line like so:
    /*this is line 60:*/ $url = trim('http://gravenimage.us/temp/') . trim($dirname) . '/' . trim($value);


    All I really needed to do was add trim().
     

  5. the problem with my mysql insert was that I had auto_increment turned off.
     
  6. The problem I'm working on now is this:
     
    I'm trying to upload from multiple folders of photos into the one folder, however when I reload the page, the foreach loop is loading the entire contents of the array each time. The array is composed of the entire contents of the folder labelled $dirname. So, for each reloading of the page, the mysql insert is cumulative. Whatever I'd loaded from the last folder loads again, creating a duplicate entry.
    I think I'm gonna create unique folders in the destination folder for each source folder. Any alternative suggestions about how I should handle this?

<?php // actual code for upload
$dirname = $_REQUEST['dirname'];
$taken = $_REQUEST['taken'];
$location = $_REQUEST['location'];
$subject = $_REQUEST['subject'];
$urldirectory = $_REQUEST['urldirectory'];
if(!(file_exists($dirname) && is_dir($dirname))) { // confirm that the directory exists, and that it is a directory
mkdir($dirname, 0777);
echo "the directory will be called ".$dirname;
} else {
echo "directory: " . $dirname;
}
if($_FILES["zip_file"]["name"]) { // pull the nmae of the zip file from the upload
 $filename = $_FILES["zip_file"]["name"];
 $source = $_FILES["zip_file"]["tmp_name"];
 $type = $_FILES["zip_file"]["type"];

 $name = explode(".", $filename); //format the filename for a variable
 $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
 foreach($accepted_types as $mime_type) {
		 if($mime_type == $type) {
				 $okay = true;
				 break;
		 }
 }

 $continue = strtolower($name[1]) == 'zip' ? true : false; // let user know if the zip file has not been uploaded
 if(!$continue) {
		 $message = "The file you are trying to upload is not a .zip file. Please try again.";
 }
 $target_path = $dirname."/".$name; // get the $target_path variable to for the move_uploaded_file() function.
 if(move_uploaded_file($source, $target_path)) { // this block extracts the zip files and moves them to the $dirname directory
		 $zip = new ZipArchive();
		 $x = $zip->open($target_path);
		 if ($x === true) {
				 $zip->extractTo($dirname."/");
				 $zip->close();
				 unlink($target_path);
		 }
				 $message = "Your .zip file was uploaded and unpacked.";
				 //use glob to find all the files that have been unzipped into the directory, and then do a foreach loop that enters the image file locations into your mysql database
				 require_once 'connect.php';
				 echo '<html>';

				 echo '<html>';
				 {$images = array(); // this clears the array by initializing between each reload of the page. Without this, each separate folder being uploaded would accumulate in the array and be uploaded multiple times.
				 $images = scandir($dirname); //use scandir to find all the files that have been unzipped into the directory, and then do a foreach loop that enters the image file locations into your mysql database
				 foreach ($images as $value) {
					 if ($value!='.' && $value!='..') {
/*							 $url = trim($urldirectory)."/".trim($value);*/
						 echo '<img src="http://gravenimage.us/temp/' . $dirname . '/' . $value . '"< /img>';
						 $url = trim('http://gravenimage.us/temp/') . trim($dirname) . '/' . trim($value);
						 $insert_sql = "INSERT INTO pics (taken, location, subject, url) VALUES ('$taken', '$location', '$subject' , '$url');";
								 if (mysql_query($insert_sql)) {
												 echo "$value"." inserted successfully!";
										 } else {	
												 echo "$value"." not inserted";
												 echo $insert_sql . '<BR>' . mysql_error();
										 }
										 }

								 unset($images); // destroys the $images variable, so it doesn't accumulate the next time you upload another folder.
									 }
									 }
						 echo '</html>';

								 } else {	
				 $message = "There was a problem with the upload. Please try again.";
		 }
 }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
if($message) echo "<p>$message</p>";
if($taken) echo "<p>pictures taken on: " . $taken . "</p>";
if($subject) echo "<p>subject: " . $subject . "</p>";
if($location) echo "<p>location: " . $location . "</p>";
?>
<form enctype="multipart/form-data" method="post" action="upload2.php">
 <label for="dirname">Directory to use?: </label> <input name="dirname" size="20" type="text" value="<?php echo $dirname; ?>" /><br />
<label for="taken">When was this taken?:</label> <input name="taken" size="20" type="text" value="<?php echo $dirname; ?>" /><br />
<label for="location">Where was this taken?</label> <input name="location" size="20" type="text" /><br />
<label for="subject">subject?</label> <input name="subject" size="20" type="text" /><br />
<input type=hidden name="urldirectory" value="<?php echo "http://gravenimage.us/temp/".'$dirname;' ?>" />
<label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
<br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>

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.