I am encountering the following error message saying:
Warning: move_uploaded_file(uploads/computer-science1.jpg): failed to open stream: No such file or directory....
while trying to move a file to a directory. above code provided.
I have also enabled the uploaded file in FileZilla to be writable and executable, is there another way of solving particular problem?
Thank you.
<?php
$upload_url = "uploads/" . $_FILES["myfile"]["name"];
$filename = $_FILES["myfile"]["name"];
move_uploaded_file($_FILES["myfile"]["tmp_name"], $upload_url);
$conn = mysqli_connect("yourhost", "username", "password", "databasename");
$sql = "INSERT INTO images (image_url, image_title) VALUES ('$upload_url','$filename') ";
$imageresults = mysqli_query( $conn,$sql );
$sql = "SELECT image_url, image_title from images";
if ($imageresults = mysqli_query( $conn, $sql )) {
while ( $currentimage = mysqli_fetch_assoc($imageresults ) ) {
echo "<div><img src= '" . $currentimage['image_url'] . "'
width='200' height='200'/><br/>" .
$currentimage['image_title'] . "</div>";
}
}
} catch( Exception $e ) {
echo $e->getMessage();
}
Your PHP script must be in a directory which contains a sub-directory, uploads/, which is writeable.
It seems as if you are attempting to move the file from its temporary location to a folder (uploads/) that either doesn't exist or that the script is unable to write to.
When you've got this message :
Warning: move_uploaded_file(uploads/computer-science1.jpg): failed to
open stream: No such file or directory....
It means that the location of the file or the location of the destination directory is not good. Maybe you can check your link path of "uploads/computer-science1.jpg"
You can use a test to check it :
if(move_uploaded_file($yourFile, $yourlocation)) {
echo 'Good ! ';
} else {
exit('Error !');
}
Related
I'm new to PHP and I'm learning from Headfirst PHP and MySQL book.
I've made a HTML form so the user can upload a file (in this case an image file).
Here is the code:
<?php
define('GW_UPLOADPATH', 'guitar/images/');
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$score = $_POST['score'];
$screenshot = $_FILES['screenshot']['name'];
if(!empty($name) && !empty($score) && !empty($screenshot)) {
$target = GW_UPLOADPATH . $screenshot;
if(move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) {
$dbc = mysqli_connect('localhost', 'root', '', 'guitar')
or die('Error connecting to MySQL server');
$query = "INSERT INTO guitarwars VALUES (0, '$name', '$score', NOW(), '$screenshot')";
$result = mysqli_query($dbc, $query) or die('Error querying database.');
echo '<p>Thanks for adding your new high score!</p>';
echo '<p><strong>Name:</strong> ' . $name . '<br />';
echo '<strong>Score:</strong> ' . $score . '</p>';
echo '<img src="' . GW_UPLOADPATH . $screenshot . '" alt="Score image" /></p>';
echo '<p><< Back to high scores</p>';
$name = "";
$score = "";
$screenshot = "";
mysqli_close($dbc);
}else{
echo '<p class="error">Sorry, there was a problem uploading your screen shot image.</p>';
}
}else{
echo '<p class="error">Please enter all of the information to add ' .
'your high score.</p>';
}
}
?>
When I put the index.php & images folder in the root folder of server everything is ok.
But when I create a directory (in this case: guitar folder) in htdocs folder (server root folder) and put project files in that directory the problem shows up.
Problem is that when user upload image file it does NOT go to images folder and this error shows up:
Warning: move_uploaded_file(guitar/images/jacobsscore.gif): failed to open stream: No such file or directory in C:\xampp\htdocs\guitar\addscore.php on line 35
Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\phpE3BE.tmp' to 'guitar/images/jacobsscore.gif' in C:\xampp\htdocs\guitar\addscore.php on line 35
the error is on line 27 "getimagesize" function , I upload the image in the same "path" , but when i proceed with the actions , i got this error everytime , and the path to the folder where the image is okay
php
//connect to the database
$link = mysql_connect("localhost" , "333" , "3333")
or die( "could not connect " . mysql_error() );
mysql_select_db("moviesite" , $link)
or die ( mysql_error() );
// make variables available
$id = $_REQUEST['id'];
if (isset($_REQUEST['mode']))
{
$move = $_REQUEST['mode'];
}
else
{
$mode = '' ;
}
// get info on the pic we want
$getpic = mysql_query("SELECT * FROM images WHERE image_id = '$id' ")
or die( mysql_error() ) ;
$rows = mysql_fetch_array($getpic);
extract($rows);
$image_filename = "upload/" . $image_id . ".jpg" ;
list($width, $height, $type, $attr) = getimagesize($image_filename);
?
try to check first if the file exists:
if (!file_exists($image_filename)){
echo "file not found!";
exit;
}
In this php code I want to customize the image upload destination. with this php file, I have directory called uploads. I want to add all my uploaded images to this directory and store path in db. how can I do this?
<?php
// Assigning value about your server to variables for database connection
$hostname_connect= "localhost";
$database_connect= "image_upload";
$username_connect= "root";
$password_connect= "";
$connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
#mysql_select_db($database_connect) or die (mysql_error());
if($_POST) {
// $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.
if ($_FILES["file"]["error"] > 0) {
// if there is error in file uploading
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
} else {
// check if file already exit in "images" folder.
if (file_exists("images/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
//move_uploaded_file function will upload your image. if you want to resize image before uploading see this link http://b2atutorials.blogspot.com/2013/06/how-to-upload-and-resize-image-for.html
if(move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"])) {
// If file has uploaded successfully, store its name in data base
$query_image = "insert into acc_images (image, status, acc_id) values ('".$_FILES['file']['name']."', 'display','')";
if(mysql_query($query_image)) {
echo "Stored in: " . "images/" . $_FILES["file"]["name"];
} else {
echo 'File name not stored in database';
}
}
}
}
}
?>
currently when I run the upload
I am getting warnings
Warning: move_uploaded_file(images/1409261668002.png): failed to open stream: No such file or directory in D:\xampp\htdocs\image-upload\index.php on line 29
Warning: move_uploaded_file(): Unable to move 'D:\xampp\tmp\php1C1F.tmp' to 'images/1409261668002.png' in D:\xampp\htdocs\image-upload\index.php on line 29
You must specify a correct path, the 'images/1409261668002.png' path doesn't exist if you dont create them and don't specify them.
if(move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]))) { .... }
You must specify the absolute path
You can use below code:
$image=basename($_FILES['file']['name']);
$image=str_replace(' ','|',$image);
$tmppath="images/".$image;
if(move_uploaded_file($_FILES['file']['tmp_name'],$tmppath))
{...}
Let me know if you have any query/concern regarding this.
I'm writing a script to upload user input and images to a place in my file system (currently on a different server).
I get an error
move_uploaded_file(images//test.pdf): failed to open stream: Permission denied in ..
$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_USERNAME, DB_USERNAME, DB_PASSWORD);
$title = $_POST["title"];
$authors = $_POST["authors"];
$description = $_POST["description"];
$price = $_POST["price"];
$uploads_dir = "images/";
$tmp_name = $_FILES["image"]["tmp_name"];
$name = $_FILES["image"]["name"];
$tmp_name2 = $_FILES["content"]["tmp_name"];
$name2 = $_FILES["content"]["name"];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
move_uploaded_file($tmp_name2, "$uploads_dir/$name2");
$file_path=$uploads_dir. basename( $_FILES["image"]["name"]);
$file_path2=$uploads_dir. basename( $_FILES["content"]["name"]);
$stmt = $dbh->prepare("INSERT INTO books (title,authors,description,price,image, content)
VALUES ('$title', '$authors', '$description', '$price',$file_path', '$file_path2')") ;
$stmt->execute();
I've looked at similar questions on here but the answers haven't concluded my problem. I have tried the $uploads_dir variable as images/ and as its complete path /project/folder/user/images/ both give the same error.
Note: this php script is in the parent folder of images/.
Your problem is clearly related to permissions. The user under which the php is running doesn't have permissions to write in this images folder. Change the owner of the folder or change the permissions.
It looks like a File Permission error. You can check whether a directory is writable or not by,
$directoryName = "specify your directory name here";
if(is_writable($directoryName){
echo "Directory is writable"; // Returns TRUE if it's writable.
}
else{
}
More on here.
I have a simple script that all I need it to do is create a directory with the name of the GET variable. When I run this script, it doesn't seem to create the directory. I would like this directory to be in the same directory as the PHP file.
$dir = $_GET['dir'];
umask(000);
mkdir($_SERVER['DOCUMENT_ROOT']."/".$dir."/",0777);
Put some error handling in there. Most of the time the error is self evident. The following snippet, lifted from PHP manual, shows you how.
$rs = #mkdir( $dirPath, 0777 );
if( $rs )
{
// success
}else{
// print error information
echo 'an error occurred. Attempting create folder';
echo '<br>dirPath: ' . $dirPath;
echo '<br>php_errormsg: ' . $php_errormsg;
}