How to show image from folder using url - php

i am using php code for creating folder at run time where i am storing some images.
when i try to accecss these image it does not show images asa well as folder name using appropriate URL.
but when i logedin using cpanel it shows folder name.
Here is my code
$dir="1buyerseller";
$desired_dir=$email;
$filename= $dir.'/'.$email.'/'.$file_name;
//echo $filename;
$query="INSERT into buysellsetailads (id,ads,cid) VALUES('seller','$filename','$cid'); ";
if(empty($errors)==true){
if(is_dir($dir.'/'.$desired_dir)==false){
mkdir("$dir/$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$dir/$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"$dir/$desired_dir/".$file_name);
}else{ // rename the file if another one exist
$new_dir="$dir/$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
mysql_query($query);
}
Using this code i am creating folder and uploding image into this folder.
whnen i try like this
http://www.xpokerala.com/admin/1square/
Here some folder are showing which created hardcoded but dyanamic created not showing
Why i dondt no.
How can i resolve this issue
Thanks

$query="INSERT into buysellsetailads (id,ads,cid) VALUES('seller','$filename','$cid'); ";
should this be buysellretailads?

Related

hello I wanna use php_move_upload_file in order to move image from my temp folder to a permanent folder

hi this is the function that upload image inside temp location and save location to session for forther use
function uploadPhoto()
{
$rawImage = $_FILES['advPhoto'];
$uploader = new ImageUploader($rawImage);
$uploader->moveToProjectTempFolder();
//1 save the current image in sassion (save the attachment class inside seesion)
$uploader->saveInSession();
// $temperrary = $uploader->CurrentImgTemperraryLocation();
//2 send reponse the current image location
AjaxHelper::sendAjaxResponse("images/temp/" . $uploader->CurrentImgTemperraryLocation());
//create image tag and set the image source the "temp uploaded image path"
// done
//when the mail form is submitted
//loop through session array
//move the user uploaded/approved images to permanent folder
//save image information inside DB
}
here is the function that cause problem I wanna move the picture from temp folder to permanent location but the php_move_uploaded_file() doesn't work in my case I don't really know what is the problem please help me if you know what is the problem thnks .
function saveAdv()
{
$advTitle = $_POST['advTitle'];
$advContent = $_POST['advContent'];
if (!empty($advTitle) && !empty($advContent)) {
if (DataValidation::isOnlyPersianOrEnglish($advTitle) &&
DataValidation::isOnlyPersianOrEnglish($advContent)) {
DBconnection::insertRow('ADVERTISEMENT', ['title', 'Advertisement', 'advDate'],
[$advTitle, $advContent, date('y/m/d h:i:s')]);
// AjaxHelper::sendAjaxResponse("success");
$projectTemp = $_SESSION['ADVERTISEMENT']['Img'];
move_uploaded_file(
$projectTemp,
DOC_ROOT . "/images/advertisementImg/"
);
AjaxHelper::sendAjaxResponse($projectTemp);
}
} else {
AjaxHelper::sendErrorMessage(AjaxHelper::EMPTY_EMAIL_OR_PASSWORD);
}
}
I don't get any error I've already debuged many times but no warning and no errors at all and the location of the folders are completely correct and also there is no permission problems.
The move_uploaded_file() works pretty well at first step that I move image from system temp location to my project temp location, but doesn't work when I wanna move the image from project temp location to permanent location.
move_uploaded_file() is only for moving files which have just been uploaded in a POST request and are stored in the system temp location. As the documentation (https://php.net/manual/en/function.move-uploaded-file.php) states, it first checks whether the file is a valid upload file meaning that it was uploaded via PHP's HTTP POST upload mechanism (that's a direct quote from the docs). If it's not valid by that definition, it fails.
So, if you're trying to use move_uploaded_file() to copy files from other locations (not the system temp location) which have not been directly uploaded to that location in the current request, then it won't work. Use PHP's general file manipulation functionality for moving other files around, using the rename() function (see https://www.php.net/manual/en/function.rename.php for details).

Upload images to temporary image folder

I have a hosted website and i want to upload my images to image directory and link the image to mysql database .
What i did:
Creating form as :
<form method=POST align=center enctype="multipart/form-data">
<input type=file name=image>
Php code :
$uploaddir = 'images/';
// echo $uploaddir;
$target = "$uploaddir".basename($_FILES['image']['name']);
echo $target;
$image = $_FILES['image']['name'];
$sql="insert into News2(image_News2)
values($image)";
$res=mysqli_query($con,$sql);
copy($_FILES['tmp_name']['name'],$target,true);
What this code actually do is only inserting the image into the database not copying the image into the images folder.
What i searched for and found :
You shouldn't specify a http:// URL as $uploaddir but a path relative to the path where the php script is running from.
So i what i really wanted to do is :
1- Copying the image into image folder directory on website.
2- Linking the copied image to the database.
you're missing the name ïmage"
not:
copy($_FILES['tmp_name']['name'],$target,true);
but:
move_uploaded_file($_FILES['image']['tmp_name'], $target);
And maybe you should try the move first, before inserting it in the database, should that action fail (or filesize = 0) etc.
do check on both the file actions and the db actions, should they fail: rollback
extra:
is only 1 column present in your database table?
is it wise to use the uploaded file's name? Duplicates can happen.
I would after checking the file insert the original name into the database. Get the autoincrement id of that record. rename the file to ./1234.jpg
through the database you can then later use the original name. But this numbering might go too var for a simple application
PHP Code :
if (isset($_FILES['image']['name']) && $_FILES['image']['error']
== 0) {
$uploads_dir = '/uploads';
$temp_file = $_FILES['image']['tmp_name'];
$name = basename($_FILES["image"]["name"]);
if(move_uploaded_file($temp_file, "$uploads_dir/$name")){
//Fire Insert Query For Insert image name in your database
}
else
{
echo "Error !! , Your image not uploaded.";
}
}
?>
as per your given data you must close Form tag then give form action ="" attribute to specify url . and if your oprating system is linux then check folder permission in set or not , also check your database table if field name is set unique or not ?
then write below code in given url file

File upload-how to create destination folder with user name

I need to upload file using php and I done it...Now my problem is I want to create a new folder for every user let me explain to you how??
My task is for user can login and access their account and file uploads. If the user will upload any file it goes to my destination folder named as 'uploads'. Now I want to create new folder inside the uploads folder with the particular username who uploads the file... so I want to create new folder for each and every users with their username... Can anyone tell me how to do this???
This is my php code for destination :
if(move_uploaded_file($_FILES['upl']['tmp_name'], '../uploads/'.$_FILES['upl']['name']))
{
echo '{"status":"success"}';
exit;
}
Thanks in advance
What you're looking for is the mkdir function. You can create a directory by using:
mkdir('/path/to/dir', 0700);
as noted in the PHP documentation.
Try with -
//check if the folder not exists then create it
if (!file_exists('<rootpath>/<username>')) { //<rootpath> will be the path from document root and <username> will be the username you want
mkdir('<rootpath>/<username>');
}
if(move_uploaded_file($_FILES['upl']['tmp_name'], '<path>/<username>/'.$_FILES['upl']['name'])) // <path> be the relative path
{
echo '{"status":"success"}';
exit;
}
Suppose You want to upload the file inside uploads/username folder.
$path = $_SERVER['DOCUMENT_ROOT'].'/uploads';
Now create the folder -
mkdir($path."/".$username); //$username be the username you want
Now upload the file
move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$username.'/'.$_FILES['upl']['name'])
Use mkdir() function to create a folder upon user registration.
Upon login create a $_SESSION variable storing the username.
When using move_uploaded_file() function add session variable to path.
move_uploaded_file($_FILES['upl']['tmp_name'], '../uploads/'.$_SESSION['username'].'/'.$_FILES['upl']['name'])
http://php.net/manual/en/function.mkdir.php
http://php.net/manual/en/reserved.variables.session.php
Thanks SGT... I got the answer with this code
$userfolder = $_SESSION['email'];
//echo $userfolder;
$path = $_SERVER['DOCUMENT_ROOT'].'register/uploads';
if (!file_exists('$path')) {
mkdir($path."/".$userfolder);
}
if(move_uploaded_file($_FILES['upl']['tmp_name'], '../uploads/'.$userfolder.'/'.$_FILES['upl']['name'])){
echo '{"status":"success"}';
exit;
}
Thanks Again

PHP letting the code add the uploader's last name to the image file he is uploading from a form

Got the server, got the domain, got the code, getting the images successfully, making the products for the customers from the image files they upload. Yay!
Problem: all my image names are image_0001 etc.
Customers can't rename image files from iPhones and do not care to from PCs.
So I was thinking about putting a short form on the upload page asking for customer's last name and having the PHP code attach that name to the image file(s) being uploaded.
If it's not possible, I'm sorry for the inconvenience.
You can rename files after they have been saved to your server, check out the PHP manual for the rename function - http://www.php.net/manual/en/function.rename.php, or just while you are moving them from the tmp directory, you can specify a different name for the uploaded file. See http://www.php.net/manual/en/function.move-uploaded-file.php
Be careful to include something in your code for dealing with naming conflicts.
This one might help :
$imagename = basename($_FILES['file']['name']);
$ext = pathinfo($imagename , PATHINFO_EXTENSION); //we want to change the file name but not the extension
$newImagename= $imageName.$username.'.'.$ext; //assuming you hold the username in $username
if (move_uploaded_file($_FILES['file']['tmp_name'], "/path/{$newImagename}"))
{
....
}

Saving a PDF file using PHP

I have a problem with saving PDF files to folders on my server. The code worked at one time and now it doesn't. What I want it to do is to check if someone is trying to upload a PDF when a form is submitted, and if there is a PDF in the file field it uploads it and then saves the path to the mysql database. Code is below:
if (!empty($_FILES['pdf'])){
$idir = "../files/PDF/"; //my directory file is supposed to be saved in
$randomd=rand(0000000,9999999); //creates a random number as filename
$domain = "http://".$_SERVER['HTTP_HOST'];
$file_ext = strrchr($_FILES['pdf']['name'], '.'); grabs file extension. my code checked if the file was a pdf a different way and neither seems to work.
$destination=$randomd.$file_ext; //new filename
if ($file_ext=='pdf') {
move_uploaded_file($_FILES['pdf']['tmp_name'], "$idir" . $destination);
$pdf= $domain."/files/PDF/".$destination; } else { echo("File type not supported.");
mysql_query("UPDATE tbl_listings SET pdf='$pdf' WHERE listing_id='$lid'");
}
The if not empty does not work and it always tries to upload a file, but when I check the folder nothing is in there and it doesnt update the mysql.
$_FILES['pdf'] will never be empty(when the form has been submitted), no matter if a file has been selected or not, it will always return an array.
Check $_FILES['pdf']['error'] , it will be 4 when no file has been uploaded.

Categories