Currently I am using file_get_contents to get contents of an HTML page I have.
if(file_exists($fileName)) {
echo "file exists!";
$current = file_get_contents("./docs/page1.html");
echo $current;
} else {
echo "file doesn't exist";
file_put_content($fileName, "testing creating a new page");
}
when the HTML file exists, I store the content using file_get_contents in variable $current and then echo current. However, nothing displays. Just a black page (which I'm guessing is the css in the html page)
If I can't spit out the page, is there a way to extract each element of the html page (divs, etc)?
file_put_content is not a function. Try file_put_contents
Also look at the changes i did to your script. It will give you information if the file can be created/read or not.
<?php
$fileName = './docs/page1.html';
if($current = file_get_contents($fileName)){
echo "file exists!";
echo $current;
} else {
echo "file doesn't exist";
if (file_put_contents($fileName, "testing creating a new page")){
echo 'created a new file!';
} else {
echo 'Error, couldnt create file! Maybe I dont have write permissions?';
}
}
?>
I tested this script, and it works unless it doesnt have file permissions. So make sure you set file permissions on the folder containing the file as well.
You are showing the page, but are probably lacking content on the page that uses relative paths, such as CSS and images.
If you wish to only use part of the page, I suggest looking into DOMDocument.
http://www.php.net/manual/en/domdocument.loadhtml.php
Related
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 4 years ago.
This may be something so stupidly simple I'm not seeing it. Doing a file upload & rename, then sending it to a confirmation page. File upload & rename works perfectly. But when the script is finished, I get a blank page and it does not redirect to mypage.php. I suspect the last IF statement. Anyone see what I am missing?
<?php
require "db_conn.php";
$ID=$_POST['ID'];
if ($_FILES['wimage']['error'] > 0)
{
echo 'Problem: ';
switch ($_FILES['wimage']['error'])
{
case 1: echo 'File exceeded upload_max_filesize'; break;
case 2: echo 'File exceeded max_file_size'; break;
case 3: echo 'File only partially uploaded'; break;
case 4: echo 'No file uploaded'; break;
}
exit;
}
// put the file where we'd like it
$upfile = $SCIMAGEPATHLISTINGS.'/'.$_POST['ID'].'.jpg';
if (is_uploaded_file($_FILES['wimage']['tmp_name']))
{
if (!move_uploaded_file($_FILES['wimage']['tmp_name'], $upfile))
{
echo 'Problem: Could not move file to destination directory';
exit;
}
}
else
{
echo 'Problem: Possible file upload attack. Filename: ';
echo $_FILES['wimage']['name'];
exit;
}
if (move_uploaded_file($_FILES['wimage']['tmp_name'], $upfile))
{
header ("Location: mypage.php?ok=add&Address=$ID");
}
?>
See the link which says
Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP.
It is a very common error to read code with include, or require,
functions, or another file access function, and have spaces or empty
lines that are output before header() is called. The same problem
exists when using a single PHP/HTML file.
In your case there are output before header() which may generating error and it may possible that your error reporting is off thus giving empty page
Found a solution. Got rid of the original else statement and replaced it with my header code. Now it works like I needed it to with no errors. Thank you all for you assistance!
if (is_uploaded_file($_FILES['wimage']['tmp_name']))
{
if (!move_uploaded_file($_FILES['wimage']['tmp_name'], $upfile))
{
echo 'Problem: Could not move file to destination directory';
// exit;
}
else
{
echo header ("Location: mypage.php?ok=add&Address=$ID");
}
}
I am just stuck today at a wall of confusion and I'm hoping someone will be able to assist :)
I have a database full of basic projects, and within that table are attributes like Project Name, Project Number, Project Image, etc. I am able to enter new projects / display existing projects / etc. without issue.
My problem seems to come up when I want to Edit a project. My thoughts were that I would have to create an IF statement to find out if there's a new file uploaded or not, and either set the new file name in the database if there is, or keep the old in the database if there isn't.
I've been playing around with this for days, and I think I started off a bit too far ahead of myself. I've started breaking down to basics and I'm getting stumped with my IF statement, it feels like it's backwards? Does this make sense?
Examples:
if (isset($_COOKIE["OldProjectImage1"])){$OldProjectImage1 = $_COOKIE["OldProjectImage1"];}
if(isset($OldProjectImage1)){
echo 'Your Browser Cookies are not enabled';
} else if(isset($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES isset';
} else if(!empty($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES empty';
}
Now in searching StackExchange I found that we have to do the statement on the COOKIE portion instead of the Variable as I did above, but it also similarly fails.
if (isset($_COOKIE["OldProjectImage1"])){$OldProjectImage1 = $_COOKIE["OldProjectImage1"];}
if(!empty($_COOKIE['OldProjectImage1'])){
echo 'Your Browser Cookies are not enabled';
} else if(isset($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES isset';
} else if(!empty($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES empty';
}
And I've also tried with isset
if (isset($_COOKIE["OldProjectImage1"])){$OldProjectImage1 = $_COOKIE["OldProjectImage1"];}
if(isset($_COOKIE['OldProjectImage1'])){
echo 'Your Browser Cookies are not enabled';
} else if(isset($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES isset';
} else if(!empty($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES empty';
}
I've tried both with my script, and they're both behaving similarly. Perhaps I am just confused at the overall process?
When I run my tests with and without cookies enabled, it always seems to skip the first part of the IF statement (with both isset and !empty) and jump to the next section. Then, similarly, it feels like the IF statement is backwards (if that makes any sense) - if I set a file to upload which populates ProjectImage1, I get "Image1 FILES empty". If I set no file to upload and submit the form, I get "Image1 FILES isset".
I thought it would essentially be, in plain English,
If cookie is empty then echo "Your Browser Cookies are not enabled"
Else if ProjectImage1 Name is set, echo "Image1 FILES isset"
Else if ProjectImage1 Name is Empty, echo "Image1 FILES empty"
but it's feeling to me like it's backwards? Am I understanding this wrong?
Thanks in advanced for any responses!
Problem lays with:
if(isset($_COOKIE['OldProjectImage1'])){
echo 'Your Browser Cookies are not enabled';
}
You check if the cookie exist, and if it does, then you say that cookies are not enabled. A bit weird. Add ! before the isset. Then the if-statement and the text are correct.
I think, but I can only assume, you want this in the end:
if (isset($_COOKIE["OldProjectImage1"])){
// I believe the variable below can also be put between the else { and } down below
$OldProjectImage1 = $_COOKIE["OldProjectImage1"];
}
if(!isset($_COOKIE['OldProjectImage1'])){
echo 'Your Browser Cookies are not enabled';
} else if(!isset($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES is not set';
} else if(empty($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES is empty';
}else {
// upload file here
}
I think you want to check the browser cookie is enabled or not?
Detect if cookies are enabled in PHP
Answer by Shiplu Mokaddim:
session_start();
if (isset($_GET['check']) && $_GET['check'] == true) {
if (isset($_COOKIE['foo']) && $_COOKIE['foo'] == 'bar') {
// cookie is working
// get back to our old page
header("location: {$_SESSION['page']}");
} else {
// show the message "cookie is not working"
}
} else {
// save the referrer in session. if cookie works we can get back to it later.
$_SESSION['page'] = $_SERVER['HTTP_REFERER'];
// set a cookie to test
setcookie('foo', 'bar', time() + 3600);
// redirecting to the same page to check
header("location: {$_SERVER['PHP_SELF']}?check=true");
}
Detect cookie in Javascript
Check if cookies are enabled
So, combine with your code and my own explanation:
<?php
session_start();
//check if a cookie test is started
if (isset($_GET['check']) && $_GET['check'] == true) {
//cookie test is started
if (isset($_COOKIE['foo']) && $_COOKIE['foo'] == 'bar') {
//cookie test success, go back to the previous page
header("location: {$_SESSION['page']}");
} else {
//cookie test fail, echo the message and continue
echo 'Your Browser Cookies are not enabled';
}
} else {
//start cookie test if a cookie test wasn't done
//check if a cookie test was done.
if (!isset($_COOKIE['foo']) && $_COOKIE['foo'] == 'bar') {
//start a cookie test if a cookie test wasn't done
$_SESSION['page'] = $_SERVER['HTTP_REFERER'];
setcookie('foo', 'bar', time() + 3600);
header("location: {$_SERVER['PHP_SELF']}?check=true");
}
}
if(!isset($_COOKIE['OldProjectImage1'])){
echo "OldProjectImage1 doesn't exists in cookies.";
} else if(!isset($_FILES['ProjectImage1']['name'])){
echo "Image1 FILES is not set";
} else if(empty($_FILES['ProjectImage1']['name'])){
echo "Image1 FILES is empty";
}
?>
All I want to check is that if a image exists in the link. I load it into an iframe. It was working fine but it seems they have removed the image but a blank.gif still exits.
NOTE: The link is a different domain
I tried the following codes in vain:
<?php
$varia = file_get_contents($url);
echo $varia;
echo "<pre>";
print_r(get_headers($url));
?>
and
$variablee = get_data($url);
pr($variablee);
All I get in the output is:
HTTP ERROR 404
Problem accessing
I want to put the condition that if blank.gif exits......some condition else some other condition.
What should I do?
In my opinion, the best way to test if an URI actual is an image, is to see what getimagesize returns. If it returns an array, it is an image :
function imageExists($uri) {
$info = #getimagesize($uri);
return is_array($info);
}
if (imageExists($uri)) {
...
}
Try
$content = #file_get_contents($url);
if ($content !== false) {
// FILE EXISTS
} else {
// FILE NOT EXISTS
}
?>
You can use this code:
if(getimagesize($url))
{
//image exist
}
else
{
//image not exist
}
I am trying to create a dynamic website, the index.php includes the following code in the content area of the website:
<?PHP
// if undefined then define
if(!$od || $od == ""){
$od = "news";
}
// check if exists prep
$link = 'incs'."/".$od.$ext;
flush();
$fp = fopen($link, "r");
// check if inclusion file not exists then return error
if (!$fp) {
echo "An error has ocurred while processing your request. The file linked as ?od=".$od." does not appear to exist.";
}
// if exists then return parse
else {
fclose($fp);
include 'incs'."/".$od.$ext;
}
echo '</body>'."\n";
echo '</html>'."\n";
?>
I also have various links throughout the site to pages like register, login, etc.
Those links point to pages like ?od=register, ?od=login, etc.
The website will pull the default file for me, news, and display that in my content section of my website, but when I click register, the url in the address bar DOES change to /?od=register, but the default news remains in the content section, is there an error in the code above? Or am I just missing something?
P.S. $ext is defined in my config file as inc.php, which is included at the top of the index page.
Besides the fact that this is very insecure - you are making a GET request, access the variables through the $_GET array ie $od = $_GET['od']
I believe you need to define $od with a $_GET['od'] or $_REQUEST['od']
$od = $_GET['od'];
// if undefined then define
if(!$od || $od == ""){
$od = "news";
I'm trying to write a script to upload a script to remove an image from my server. I keep getting the error message. Can anyone find anything wrong with this code?
// Delete image
if(isset($_GET['deleteImg']) && !empty($_GET['deleteImg']) && $_GET['deleteImg'] == true)
{
// Get imagepath from database
$result = mysql_query("SELECT image FROM frankkluytmans WHERE id=$id");
$imageDeletePath = mysql_fetch_assoc($result);
// Delete image from server
if(unlink($imageDeletePath['image']))
{
// Continue if image has been reset in database
if(mysql_query("UPDATE frankkluytmans SET `image`='' WHERE id=$id")){
// once deleted, redirect back to the view page
header("Location: index.php");
}
}
else
{
?>
<script type="text/javascript">
window.alert('This image could not be deleted.';
</script>
<?
}
}
I guess your error is in the path of your unlink() function, as you said actual field is like /gfx/image.png wich doesn't look to me like an absolute path, correct me if i'm wrong.
To delete the file and use unlink() directly your script should be in the same folder as the image. So i think it would be better to set an absolute path to your entry like
$path_abs = ' /customers/d/8/e/frankkluytmans.nl/httpd.www/testsite/cms'; //is the `gfx` folder inside `cms` folder? if it is this will work otherwise you have to change
if(unlink($path_abs . $imageDeletePath['image']))