I can insert the folder path into the database but the picture file doesn't seem to move into that folder?
$pic = $_POST['pic'];
$picName= $_FILES['pic']['name'];
$type = $_FILES['pic']['type'];
$tmp = $_FILES['pic']['tmp'];
$picPath = "/pictures/";
if(is_uploaded_file($tmp)) {
if(move_uploaded_file($tmp, $picPath . $picName)) {
echo "congrats! Image is uploaded.";
}
else {
echo "Sorry, couldn't move your picture.";
}
}
else {
echo "Sorry, couldn't upload your picture.";
}
$picPath = $picPath . $picName;
mysql_query("INSERT INTO User(pic) VALUES ('$picPath')");
I get this echo message: Sorry, couldn't upload your picture.
The php files is saved on public_html folder, and I have a pictures folder where I want to move the users pictures into.
The insertion works as I can store the $picPath in my database, but the picture don't get stored in my folder.
Try replacing
$tmp = $_FILES['pic']['tmp'];
with
$tmp = $_FILES['pic']['tmp_name'];
1) check folder permissions if its writable or not.
2) make sure your path is same with the same folder name in code as well.
3) Try to change from this $picPath = "/pictures/"; to something like this $picPath = "pictures/"; removed forward slash.
Related
So i want my page to show the image whose path I am getting from the mysql database and displaying on the same screen. This is my code, I have tried everything, please let me know where I'm going wrong.
while ($row = mysqli_fetch_array($return_data)) {
echo "ID:".$row['demo_id']."<br>";
echo "Name: ".$row['demo_name']."<br>";
echo "Version: ".$row['demo_version']."<br>";
echo "Details: ".$row['demo_details']."<br>";
echo "File Link: ".$row['file']."<br>";
$new = $row['file'];
echo '<img src = \"$new\"/>';
}
mysqli_free_result($return_data);
echo "Data retrieved successfully!"."<br>";
?>
<img src = "<?php echo $new?>">
echo "File Link: " returns me the whole path of the uploaded file.
How do I render the image at that path in the same page?
neither of the image tags are working. Thanks in advance!
edit
File Link: C:/Apache24/htdocs/demo_webpages_project/neweruploads/footer.jpg
this is the path I get as an output.
Basically this is the folder where I have uploaded the image from another php file
<?php
//this module is used to temporarily store the uploaded file to the server
$target_dir = "random/"; //we randomly assign a value to the upload target directory
$target_file = $target_dir . basename($_FILES["image_file"]["name"]); /*here ["name"] is the original name of the file before it was updated
target file is assigned this name by appending it to the $targer_dir
now target_file is the uploaded file name along with the path*/
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);/*this returns various kind of info about the path for eg Directory, Basename
in our case it returns extension of the file*/
//this sub module is to check whether the file is really an image file
if(isset($_POST["submit"])) { //isset is used to confirm whether the value has actually being submitted
$check = getimagesize($_FILES["image_file"]["tmp_name"]);/*here ["tmp_name"] is the location of temporary file on server
getimagesize confirms image format by returning dimensions etc*/
if($check !== false) {
echo "A file is of an image format<br>";
}
else {
echo "The file is not an image!<br>";
}
}
//Test module to upload files to a destination directory and check whether they have been uploaded or not
if (is_uploaded_file($_FILES['image_file']['tmp_name']) && $_FILES['image_file']['error']==0) { /*code to check two things: 1. whether the file exists in the temp memory 2. whether the file has any error*/
$path = 'C:\Apache24\htdocs\demo_webpages_project\neweruploads\\' . $_FILES['image_file']['name']; /*this sets the destination directory(along with expected file name)*/
if (!file_exists($path)) { //if the file does not exists at that path
if (move_uploaded_file($_FILES['image_file']['tmp_name'], $path)) { //use the php file move function to move it
echo "The file was uploaded successfully."; //success
}
else {
echo "The file was not uploaded successfully."; //failure
}
}
else {
echo "File already exists. Please upload another file."; //detects existence of file with exact same name
}
}
else {
echo "The file was not uploaded successfully."; //if any problem with original uploading
echo "(Error Code:" . $_FILES['image_file']['error'] . ")";
}
?>
Does this help?
edit 2
http://localhost:8080/demo_webpages_project/download.php?project_name=footer&version=&submit=Search
this is my local directory path.
the solution you provided is allowing me to read images which are in the demo_webpages_project folder pointing directly there), not to neweruploads folder
If your uploaded files are stored in the neweruploads subdirectory, then replace this code:
$new = $row['file'];
echo '<img src = \"$new\"/>';
By this one :
$new = basename( $row['file'] ); // GET FILE NAME ONLY, GET RID OF PATH.
echo '<img src = \"neweruploads/$new\"/>'; // FILENAME WITH UPLOAD PATH.
▲
I have a problem with moving uploaded files.
<?php
$image_name = $_FILES['image']['name'] ;
$target_file = "../uploads/$image_name";
$targetFileForItem = "uploads/$image_name";
move_uploaded_file($_FILES['image']['tmp_name'], $target_file);
$sql = "INSERT INTO items (name , description,`price`, `country`, `release`, `condition`, `image`)
VALUES ('$name','$description','$price', '$country', '$date', '$condition', '$targetFileForItem')" ;
?>
the variable $targetFileForItem works currect, and inserts into my db very well, but the file don't move into $target_filevar's folder, which is uploads. As you see I use move_uploaded_file() function, but i's not working. Any suggestions?
Write this to debug
ini_set('display_errors',1);
error_reporting(E_ALL);
If your code is ok then check file permissions you can use this
if (is_dir($target_file ) && is_writable($target_file )) {
// do upload logic here
} else {
echo 'Upload directory is not writable, or does not exist.';
}
is_writable Returns TRUE if the filename exists and is writable. The filename argument may be a directory name allowing you to check if a directory is writable
for more info read this http://php.net/manual/en/function.is-writable.php
Check your permission for upload folder it must be 775. If you are using FTP than right click on folder and change File permission of that folder to 755.
If it's localhost then it must be a path or folder name issue.
And make your code like this so you can get find out errors also.
<?php
$image_name = $_FILES['image']['name'] ;
$target_file = "../uploads/$image_name";
$targetFileForItem = "uploads/$image_name";
// if folder not exists than it will make folder.
if(!file_exists($target_file))
{
mkdir($target_file, 0777, true);
}
if(move_uploaded_file($_FILES['image']['tmp_name'], $target_file))
{
echo "file successfully uploaded";
}
else
{
echo "error in file upload";
}
?>
my code is successfully working for upload images. but problem is it fails to display uploaded images. i checked image path output is correct in html source view but image till not displaying. i think its a file permission issue. how can i upload these images with read write full permission so images can display properly.
if(isset($_POST["submit_img"])) {
$target_dir = "../assets/img/temp_img/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$randstr = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 3);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_dir.$randstr))
{
$q =mysqli_query($conn,"INSERT INTO temp_img (product_img_url) VALUES ('$randstr')");
if ($q>0) {
echo "<br>The file has been uploaded.";
}
} else {echo "<br>Error uploading your file.";}
}
//show all uploaded images
$q2 = mysqli_query($conn,"SELECT product_img_url FROM temp_img");
while ($row = mysqli_fetch_array($q2)) {
$product_img_url = $row['product_img_url'];
$img_format = ".jpg";
$broken_url = "assets/img/temp_img/";
echo '<img src="'.$main_url.$broken_url.$product_img_url.$img_format.'" class="img-rounded" alt="Cinque Terre" width="304" height="236">';
}
image missing picture
I think the following link will help you change permissions of your file.
http://php.net/manual/en/function.chmod.php
Note:
1.check whether the uploaded file is in specified folder.
2.try setting the path without "../" in the variable targetdir
Need to insert uploaded file into respected folder
Here i am creating folder based on unique id.
I am not able to insert file into the folder.
when file is uploaded both file and folder are stored separately.
if ($file_check_error == 0){
if(move_uploaded_file($file['tmp_name'],$upload_directory.$path)){
echo"inside 2"."<br>";
echo"Your File Successfully Uploaded";
}
$path_user = '/home/devestctrl/public_html/wp-content/plugins/est_collaboration/Files/'.$send_id;
if (!file_exists($path_user)) {
if (mkdir( $path_user,0766,false )) {
$path_move = $path_user."/".$path;
echo $path_user;
rename($path,$path_move);
echo "Success+++++++++++";
}
else{
echo $path;
echo "Failure+++++++++++";
}
}
}
$send id is unique id.
Please let me know where i have gone wrong?
You are messing up your logic. First you move your uploaded file
if (move_uploaded_file($file['tmp_name'], $upload_directory.$path)) {
and only than you try to create new directory
if (mkdir($path_user,0766,false )) {
and only if current user never uploaded anything you rename file moving it to other dir
rename($path,$path_move);
Correct logic:
Format uploaded file path $path = $upload_directory.DIRECTORY_SEPARATOR.$send_id
Check if dir exists file_exists($path)
If not exists, create it mkdir($path, 0766, false)
Upload file move_uploaded_file($file['tmp_name'], $path)
E.g.:
$path = $upload_directory.DIRECTORY_SEPARATOR.$send_id;
if (!file_exists($path)) {
mkdir($path, 0766, false);
}
move_uploaded_file($file['tmp_name'], $path);
Hey i have a system were in uploading a file. I have a script I've found online and it seems to work well.
Here is the PHP code:
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0))
{
//Check if the file is JPEG image and it's size is less than 350Kb
$filename = basename($_FILES['uploaded_file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && ($_FILES["uploaded_file"]["size"] < 350000))
{
//Determine the path to which we want to save this file
$newname = dirname(__FILE__).'upload/'.$filename;
//Check if the file with the same name is already exists on the server
if (!file_exists($newname))
{
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname)))
{
echo "It's done! The file has been saved as: ".$newname;
}
else
{
echo "Error: A problem occurred during file upload!";
}
}
else
{
echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
}
}
else
{
echo "Error: Only .jpg images under 350Kb are accepted for upload";
}
}
else
{
echo "Error: No file uploaded";
}
No this works fine if i want to upload a jpg file. But i want to be able to put the file into another directory. because at the moment the upload page is for admin users, they are on a subdomain called admin.mysite.com but the location i want the file to go to is in the members section which is mysite.com/members/video/
Now there are a few bits of code that im not 100% with like "dirname(FILE)" what does this do? I guessed it would get the current locations, but i've changed the whole line where so it looks like this:
$newname = '../mysite.com/members/video/'.$filename;
and
$newname = 'http://www.mysite.com/members/video/'.$filename;
But nothing. Anyone know how i can change this code so i can copy the file to a new directory?
Thanks for the help.
Change $newname to whatever location you want!
//Determine the path to which we want to save this file
$newname = dirname(__FILE__).'upload/'.$filename;
dirname(_ FILE _) returns the current directory of the file, in this case, file_upload.php
So, in this script, the $newname will save the uploaded file to /upload/name_of_new_file_uploaded.ext.
You should try to use realpath() instead dirname. Like this:
$newname = realpath("../../members/video/") . $filename;
depending on you file structure, add/remove the dots.
PS: Remember to change folder permissions, so the php can write on a folder.
For security, you should
Put the file in a location that is not accessible by the general web. /home/uploadedfiles/
Change the name of the file. Store the name of that file in a database and don't let the end users see that actual name.