Can't reference POST variable at parts of the code in PHP - php

I have a bizarre problem that I believe should have a very simple solution but, for the life of me, I can't seem to understand what's going on.
I'm grabbing a POST variable and I first check if it is posted and echo it. It is.
However, when I try to form a variable that is a full path to a filename, it doesn't recognize it and I obviously can't open the file...
I do get a warning for an Undefined variable so that's where the issue is but I can't understand why I can check the value of that variable anywhere and it's properly set.
Here's the code:
if (isset($_POST['fid'])) {
echo 'fid = ' . $_POST['fid'];
}
I form a whole path (which I echo and it is fine) to the file I'd like to open:
$fnameonly = getfilename($_POST['fid']);
echo $fnameonly; //echos fine, getfilename() worked fine
$fname = $_SERVER['DOCUMENT_ROOT'].'/uploads/' . $_SESSION["username"] . "/" . $fnameonly;
echo $fname; //also echos fine, $fname should be properly set
Now in looking at the resulting HTML, I get 2 warnings:
Notice: Undefined variable: fid in C:\web\www\csv\translatebs.php on line <i>23</i>
This is refers to the following line from the code above:
$fnameonly = getfilename($_POST['fid']);
The second warning refers to simplexml not being able to open the file since the path is incomplete.
Warning: simplexml_load_file(C:/web/www/uploads/paulohgo/): failed to open stream: No such file or directory in C:\web\www\csv\translatebs.php on line <i>31</i>
I can echo $fnameonly just fine but the warning is there.
When I try to open the file the value from $filenameonly is empty, so the I only get the path to a folder but miss the actual file name.
I don't get what I'm missing here... thanks for any help.

Related

Php Problem with move_uploaded_file. It seems everything is fine but it is not working

I am trying to use move_uploaded_file but I think everything is fine but my code is not working. Everything is working as expected even image name is being inserted into DB but it is not moving into uploads folder. Folder is there and apache2 does have access to write it.
I don't have enough reputation to post more then 8 lines so everything is on pastebin Here. The main aprt is...
$post_image_new_name = uniqid('', true). "."
.$post_image_actual_ext;
$post_image_destination = '../../uploads/';
if (is_dir($post_image_destination) && is_writable($post_image_destination)) {
$post_image_destination = '../../uploads/'.$post_image_new_name;
var_dump($post_image_destination);
move_uploaded_file($post_image_new_name, $post_image_destination);
echo "Inside move_uploaded_file section";
https://pastebin.com/Z321R76z
Try this code
line 53: move_uploaded_file($_FILES['addPost_post_image']['tmp_name'], $post_image_destination);
The first param should be the file source name.
move_uploaded_file() expects the source filename of the uploaded file. So you must use something like this:
move_uploaded_file($_FILES['addPost_post_image']['tmp_name'], $post_image_destination);

Warning: move_uploaded_file(): The second argument to copy() function

I'm working on a file upload method. But i suddenly start to get the following error, The only thing I changed was the file name. I reverted it back, but the error still persists.
Does anyone know how to solve this?
The error message is:
Warning: move_uploaded_file(): The second argument to copy()
function cannot be a directory in
/hermes/bosnaweb14a/b1717/ipg.plantationkeyartcorn/kittyrescuetnr/docs/upload.php
on line 13
Warning: move_uploaded_file(): Unable to move '/tmp/phpJki8OC' to '/' in
/hermes/bosnaweb14a/b1717/ipg.plantationkeyartcorn/kittyrescuetnr/docs/upload.php
on line 13
Fail
my php looks like this
<?php
$file_upload="true";
$file_up_size=$_FILES['file_up'][size];
$file_destination=$REQUEST['file_type'];
$file_new_name=$REQUEST['file_name'];
$file_name=$_FILES[file_up][name];
**$add="$file_destination/$file_new_name"; // the path with the file name where the file will be stored**
if($file_upload=="true"){
if(move_uploaded_file ($_FILES[file_up][tmp_name], $add)){
echo print_r($file_new_name);
}else{echo "Fail";}
}else{
echo $msg;
}
?>
Well, since you are using superglobal array $_REQUEST incorrectly (notice the underscore in the name), your $add variable evaluates to just a slash. Which is a root directory.
Hence the errors - it is a directory after all and your script probably don't have write access to it anyway (which is a good thing).

Problems with accessing files

Hello stackoverflow community, how can i solve this problem, here it is my error:
Warning: require_once(DP_BASE_DIR/classes/query.class.php):
failed to open stream: No such file or directory in
the that query.class.php is in the folder:
$_SERVER['DOCUMENT_ROOT']/classes/query.class.php
Im trying to access it from:
$_SERVER['DOCUMENT_ROOT']/modules/tasks/ajax/file.php here.
When i try to use:
require_once($_SERVER['DOCUMENT_ROOT'].'/classes/query.class.php'):
It works like a charm. But i need to acces that file using DP_BASE_DIR. So when i write:
require_once DP_BASE_DIR."/classes/query.class.php";
I get an error. How can i solve this problem?
Try to see if this makes sense to you, I have tested this out using:
test.php
<?php
define('DP_BASE_DIR', '/var/www/devspace');
if (!(defined('DP_BASE_DIR'))) { die('This file should not be called directly.'); }
require DP_BASE_DIR."/test2.php";
var_dump($string);exit;
// outputs string(4) "test"
?>
test2.php
<?php
$string = 'test';
?>
Isn't that exactly what you're doing? I feel that something else is coming into play here and being left out of the info that has been provided.
The only real issue that I could spot here is that maybe $_SERVER['DOCUMENT_ROOT'] did not hold the exact same value as your DP_BASE_DIR but as you have confirmed these to be the same I am scratching my head about other possibilities as this should just work.
EDIT:
do this right before the require:
$test = DP_BASE_DIR."/classes/query.class.php";
var_dump($test);exit;
Post the result?
Warning: require_once(DP_BASE_DIR/classes/query.class.php):
failed to open stream: No such file or directory in
Seems DP_BASE_DIR constant does not exists. If constant does not exists will use the name of constant, and you get a notice.
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."
echo Constant; // outputs "Constant" and issues a notice.

Renaming a file results in 'No such file or directory'

I've been trying to rename an image file using php using this code.
$user_id = $_POST[user_id];
$old_image_name = $_POST[old_image_name];
$image_name = $_POST[image_name];
rename('/_img/user_memes/large/user_'.$user_id.'/'.$old_image_name.'', '/_img/user_memes/large/user_'.$user_id.'/'.$old_image_name.'');
The error I'm getting is-
"Warning: rename(/_img/user_memes/large/user_2/1524957_717357634955838_1917151587_n.jpg,/_img/user_memes/large/user_2/1524957_717357634955838_1917151587_n.jpg) [function.rename]: No such file or directory in /var/sites/o/oddmeme.com/public_html/_process/post_single_meme_edit.php on line 8"
The image is definitely there. I've tried removing the / at the start and trying a few different version but nothing has worked.
I've also set permission to 777 so that shouldn't be a problem.
First of all, you have an error in your code:
You are using twice the same path in your function with $old_image_name...
Fix that and tell us if it's working.

file_get_contents() failed to open stream: No such file or directory

I am writing some php script to update the code on my sites. In order to do that I have written the following lines which checks for the update version and bring the name from where I use to distribute my updates ans then creates the link of that name. I have done something like this.
$filename = "http://www.hf-live.com/codeupdate/Get_Files_Name.php";
$contents = file_get_contents($filename);
echo $contents;
I am getting this error
failed to open stream: No such file or directory.
Even though the file is present still I am getting the same error. I have turned on my allow_url_fopen to on. The above code is working from my local host but not from the server. I have to update a major bug fix and I am stuck.. Please someone help me soon..
remove extra .php from url
$filename = "http://www.hf-live.com/codeupdate/Get_Files_Name.php";
You will get error if you are doing this way...
$filename = "marynyhof.com/Info.php"; // will give you error, which exactly you are getting
use full url like this
$filename = "http://www.marynyhof.com/Info.php"; //will work

Categories