This is the code
$query = mysql_query("SELECT avatar FROM users WHERE UserID = ".$userID."");
$row = mysql_fetch_array($query);
$user_avatar = trim($row['avatar']);
unlink($user_avatar);
but for some reason i get this error Warning:unlink();
why $user_avatar returns empty ? and if i echo it shows t_cabbbccebbfhdb.jpg
unlink remove files whereas unset is for variables.
If the variable returns empty, perhaps the query does not return any records. Did you try to run the query manually?
$query = mysql_query("SELECT avatar FROM users WHERE UserID = ".$userID."");
$row = mysql_fetch_array($query);
$user_avatar = trim($row['avatar']);
unset($user_avatar);
//if you want to unlink file then
if(!empty($user_avatar)) {
unlink($home.$user_avatar); // $yourFile should have full path to your file
}
In PHP unlink is used to delete a file, make sure you are giving right path. see here for details http://se.php.net/unlink
try unset for variables. http://se.php.net/manual/en/function.unset.php
Related
I try to delete multiple images from folder and also from database, deleting from database is working fine, problem is with deleting from folder I'm using explode function because I have store multiple images inside one column in database. It just won't delete images from folder.
$query = "DELETE FROM table_gallery WHERE id = $deleteId";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result)) {
$temp = explode(',',$row['images'] );
foreach($temp as $image){
$images[]="uploads/gallery/".trim( str_replace( array('[',']') ,"" ,$image ) );
}
foreach($images as $file) {
// Delete given images
unlink($images);
}
}
You have a few issues going on here,
1) Variable Confusion
The most notable is this:
foreach($images as $file) {
// Delete given images
unlink($images);
}
You are opening the array of $images and then dealing with each element in the array as a string reference ($file), but your unlink() command is calling the array, and you can't do that.
Unlink requires you to give it a filepath string. Not an array.
Fix:
foreach($images as $file) {
// Delete given images
unlink($file);
}
Better:
As an aside, if unlink($file); does not work, then you should check your filepath is correct. Remember the current PHP working directory is the directory the PHP script is in, so you should probably set your deleted image path to being the full server filepath such as:
unlink($_SERVER['DOCUMENT_ROOT']. $file);
To ensure you're certain of the file you're deleting.
foreach($images as $file) {
// Delete given images
// /serverfolder/accountfolder/public_html/uploads/gallery/imagename.jpg
unlink($_SERVER['DOCUMENT_ROOT'].$file);
}
2) Your Safety and Security:
Your variable $deleteId might be safe, but it might not. We can't tell from the code you show but you should be aware that if the $deleteId variable is, for example;
$deleteId = '1 OR id > 1';
// which generates:
$query = "DELETE FROM table_gallery WHERE id = 1 OR id > 1";
This is a security risk for your SQL and will delete all records on your database table (and folder). You should use Prepared Statements to ensure this doesn't ever happen.
3) Logic Confusion
you have a reference call mysqli_fetch_assoc but the DELETE SQL command will not return a result set, instead if it is successful it will simply return true.
Returns false on failure. For successful queries which produce a result set, such as SELECT, SHOW, DESCRIBE or EXPLAIN, mysqli_query() will return a mysqli_result object. For other successful queries, mysqli_query() will return true.
You need to structure your code somewhat differently, loading the images column value BEFORE you delete the row.
So, to restructure your code:
Check variables are safe.
load images column values
cycle through values and delete
Once deletion is confirmed then DELETE SQL.
For example:
$query = "SELECT images FROM table_gallery WHERE id = ".(int)$deleteId;
$result = mysqli_query($con, $query);
$abort = false;
while($row = mysqli_fetch_assoc($result)) {
$temp = explode(',',$row['images'] );
foreach($temp as $image){
$images[]="uploads/gallery/".trim( str_replace( array('[',']') ,"" ,$image ) );
}
foreach($images as $file) {
// Delete given images
if(!unlink($_SERVER['DOCUMENT_ROOT'].$file)){
// Delete failed so abort
$abort = true;
}
}
//
if(!$abort){
// all correct images on disk deleted ok so delete DB row
if(mysqli_query($con, "DELETE FROM table_gallery WHERE id = ".(int)$deleteId)){
// Yay deleted ok!
}
}
}
Closing thought:
You need to learn to read your PHP error logs which will spell out all the above.
I have below query http://localhost/barbadosparliament/result/index?qry=testing
Now i want ot get data of qry parameter from url. But not able to get data. I used $this->uri->segment and also $_GET['qry']. But still i doesn't get that record. How can i get that record.
check you have on in config/config.php
$config['allow_get_array'] = TRUE;
then try
$qry = $this->input->get('qry', TRUE);
$qry = $_GET['qry'];
$qry = $_REQUEST['qry'];
Also try url like
http://localhost.com/barbadosparliament/result?qry=testing
try this:
$qry = $this->input->get('qry');
But this should works too
$qry = $_GET['qry'];
I have been trying different things in order to get this script to query the MySql DB for the image name and then go to the directory and return the image with the same name. It seems simple in concept, but I keep getting a bunch of mixed results and none of the results return the image.
Here is the most recent code that I tried:
$sqlCar = mysql_query("SELECT img_nm FROM vehicles WHERE sold='n' ORDER BY year DESC");
/***************************
****
Code Returns '1'
******************************/
$dbImage = $sqlCar;
$dirImage = scandir($_SERVER['DOCUMENT_ROOT'].'/car-pics/');
function imgCompare($dbImage){
if ($dbImage == $dirImage[img_nm]){
echo $dirImage[img_nm];
} else {
echo "Image Coming Soon";
}
}
You're not adding $dirImage as a parameter to the function. You can solve this by adding it as a parameter, or adding the global keyword to the first line of your function like this:
function imgCompare($dbImage){
global $dirImage;
if ($dbImage == $dirImage[img_nm]){
echo $dirImage[img_nm];
} else {
echo "Image Coming Soon";
}
}
Also, what values are in img_nm in the database? scandir uses an array of index values. So $dirImage[0] would work, but $dirImage['filename.jpg'] would not.
The mysql_query function is not going to return the value you are looking for directly into $sqlCar where it is directly accessible.
you will have to do something like
$row = mysql_fetch_array( $sqlCar );
And then reference the array to get the image name
$dbImage = $row['img_nm'];
I am trying to delete an item from a folder somewhere, and I need to pass in the path of the item to unlink(). For some reason it says I am not returning a string, which I am not sure how to do.
Here is all the code:
$filePath = "Select path FROM library where id=" .$id;
unlink(mysql_query($filePath));
$library = "DELETE FROM library WHERE id =" .$id;
$files = "DELETE FROM files WHERE library_id=" .$id;
mysql_query($library) or die('Unable to update using sql '.$library.' '.mysql_error($link));
mysql_query($files) or die('Unable to update using sql '.$files.' '.mysql_error($link));
mysql_query returns a statement HANDLE, not a string. You need to fetch a row from the result:
$res = mysql_query($filePath) or die(mysql_error());
$row = mysql_fetch_assoc($res);
unlink($row['path']);
mysql_query returns a resource, not a string.
Use:
$query = mysql_query($filePath);
$result = mysql_fetch_array($query);
$path = $result['path'];
unlink($path);
The process:
Query the database and store the resource handle in a variable ($query).
Fetch the row (if there is only one) and store the row in $result.
Fetch the value for the path column.
Use that value to delete the file.
See the manual for detailed information and usage notes.
http://php.net/manual/en/function.mysql-query.php
http://www.php.net/manual/en/function.mysql-result.php
mysql_query does not actually return the values of that your query selected, but a result resource. You can use the function mysql_result(, , ) to get the wanted information.
Can you do that? I just tried but it doesnt seem to work.
I have dbc.php included at top of my page, and in dbc.php at the bottom i created this function:
function getUserInfo($id) {
$thaString = mysql_query("SELECT * FROM users WHERE id = '$id'");
$thaString2 = mysql_query("SELECT * FROM users_profile WHERE uID = '$id'");
$showUR = mysql_fetch_array($thaString);
$showURP = mysql_fetch_array($thaString2);
}
So it would be easier for me to call them instead of running the queries all the time when i need them in other pages..
But when i try to do:
getUserInfo($showInfo["bID"]);
echo $showUR["full_name"];
I dont get any result, is there a smarter way to do this, if so how?
It's an issue of scope: $showUR is set inside getUserInfo(), so it's not available to the echo outside the function. There are lots of potential modifications you could make, but you may want to assign your values into an array and then return that array:
function getUserInfo($id) {
$user = array();
$thaString = mysql_query("SELECT * FROM users WHERE id = '$id'");
$thaString2 = mysql_query("SELECT * FROM users_profile WHERE uID = '$id'");
$user['showUR'] = mysql_fetch_array($thaString);
$user['showURP'] = mysql_fetch_array($thaString2);
return $user;
}
$user = getUserInfo($showInfo["bID"]);
echo $user['showUR']["full_name"];
Your functions have to return something for the values to be used, or $showUR and $showURP will just get lost once the function exits (ie: the scope will change). Something like:
function someFunc($arg) {
return "Hello, I am {$arg}";
}
$showUR = someFunc('name');
echo $showUR;
And please don't call stuff $thaString. First because it's a misnomer (mysql_query() doesn't return a string, it returns a resource or a boolean), Second because "tha" is so lame.
Let your function return the variable.
And then use $showUR = getUserInfo(...)
If you declare them outside the function, and then as globals inside the function you should be able to use them as you are now.