/    Sign up×
Community /Pin to ProfileBookmark

Errors in PHP script

I’m currently working on a feature in my web app where I can upload a file. But everytime I upload something this errors appears:

`
array(1) { [“file”]=> array(5) { [“name”]=> string(39) “Screenshot from 2020-11-09 18-53-37.png” [“type”]=> string(9) “image/png” [“tmp_name”]=> string(14) “/tmp/phpAdl88R” [“error”]=> int(0) [“size”]=> int(452262) } } Your document was uploaded successfully
Notice: Trying to get property ‘childNodes’ of non-object in /var/www/html/fileUpload.php on line 26

Warning: Invalid argument supplied for foreach() in /var/www/html/fileUpload.php on line 28

Warning: DOMDocument::save(../super_user/overview.html): failed to open stream: No such file or directory in /var/www/html/fileUpload.php on line 46
`
Also the file is successfully uploaded but when I check in the folder it is automatically locked. I’m using Linux OS

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 09.2020 — Pretty hard to tell without seeing the relevant code. Sounds like an error from something using the [DOM extension](https://www.php.net/manual/en/book.dom.php), which seems at odds with processing a PNG image file? 🤷‍♂️
Copy linkTweet thisAlerts:
@frncsknauthorNov 10.2020 — @NogDog#1624950 The first error I got was something to the effect of "the DOM is not installed" so I Installed it and now this error appeared. I also tried to upload a pdf file and the same error appeared.
Copy linkTweet thisAlerts:
@NachfolgerNov 10.2020 — [Can't tell you anything without relevent code. ](https://stackoverflow.com/help/minimal-reproducible-example)
Copy linkTweet thisAlerts:
@frncsknauthorNov 10.2020 — @NogDog#1624950 @Nachfolger#1624954 For clarification this is the code that I wrote:
``<i>
</i>&lt;?php
ini_set('display_errors', '1');
error_reporting(E_ALL);
var_dump($_FILES);
$uploadok = true;
// check if an error occured at uploading
if (0 &lt; $_FILES['file']['error']) {
echo 'Error: ' . $_FILES['file']['error'] . '&lt;br&gt;';
$uploadok = false;
} else {
// upload was successful
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
echo 'Your document was uploaded successfully';
}
$filename = 'uploads/' . $_FILES['file']['name'];
if ($uploadok) {
// create instance od DOMDocument
$doc = new DOMDocument();
// supress errors as DOMDocument does not know
// about HTML5 elements
libxml_use_internal_errors(true);
// load HTML file
$doc-&gt;loadHTMLFile("overview-tpl.html");
// get child nodes of of the section
// containing info about the PDF document
$nodes = $doc-&gt;getElementById('doc1')-&gt;childNodes;
// loop through all nodes
foreach ($nodes as $node) {
// check if node is an element node
if ($node-&gt;nodeType == XML_ELEMENT_NODE) {
// provide class of current node
$class = $node-&gt;getAttribute('class');
// is this the a element containing the link to the PDF?
if (strstr($class, 'link-to-pdf')) {
// set attribute href to the path of the PDF
$node-&gt;setAttribute('href', $filename);
}
// is this the container for date and time?
if (strstr($class, 'time-of-issue')) {
// enter date and time
$node-&gt;textContent = strftime('Issued at %I:%M %p %d %B %Y');
}
}
}
// save document
$doc-&gt;save('../super_user/overview.html');
}

?&gt;<i>
</i>
``
Copy linkTweet thisAlerts:
@NogDogNov 10.2020 — Best guess is that whatever is in overview-tpl.html does not have an HTML element with an id value of "doc1" (e.g. something like &lt;div id="doc1"&gt;); i.e.: $nodes = $doc-&gt;getElementById('doc1') is returning FALSE, and FALSE is not an object that might have a property named childNodes. ;)
Copy linkTweet thisAlerts:
@frncsknauthorNov 10.2020 — @NogDog#1624956 Thanks for the reply. Will into it.
Copy linkTweet thisAlerts:
@frncsknauthorNov 10.2020 — @NogDog#1624956 another thing that is weird. the file is uploaded successfully and its saved to a folder that I made. but when I view in the filemanager of ubuntu. its locked
Copy linkTweet thisAlerts:
@NachfolgerNov 10.2020 — > @frncskn#1624958 another thing that is weird. the file is uploaded successfully and its saved to a folder that I made. but when I view in the filemanager of ubuntu. its locked

You probably aren't the owner of the directory. Add yourself alongside www-data or whatever it's currently set to.
Copy linkTweet thisAlerts:
@frncsknauthorNov 10.2020 — @Nachfolger#1624970 I just did that and now this are the errors:
``<i>
</i>array(1) { ["file"]=&gt; array(5) { ["name"]=&gt; string(60) "Background Check Authorization Form for Word revised (1).pdf" ["type"]=&gt; string(15) "application/pdf" ["tmp_name"]=&gt; string(14) "/tmp/phpYI8dH9" ["error"]=&gt; int(0) ["size"]=&gt; int(306125) } } Your document was uploaded successfully
Notice: Trying to get property 'childNodes' of non-object in /var/www/html/fileUpload.php on line 26

Warning: Invalid argument supplied for foreach() in /var/www/html/fileUpload.php on line 28

Warning: DOMDocument::save(../super_user/overview.html): failed to open stream: No such file or directory in /var/www/html/fileUpload.php on line 46<i>
</i>
``
Copy linkTweet thisAlerts:
@NachfolgerNov 10.2020 — @frncskn#1624972 They aren't errors, they're warnings. The distinction is significant.

Warning 1: Trying to get property 'childNodes' of non-object
  • - NogDog already gave you a debugging starting point for it.


  • Warning 2: Invalid argument supplied for foreach()
  • - Rooted in foreach ($nodes as $node) { ... }

  • - Fix warning 1 to fix this one


  • Warning 3: failed to open stream: No such file or directory
  • - Rooted in $doc-&gt;save('../super_user/overview.html')

  • - That's a plaintext error. Saving the document in that location isn't possible. Does the directory "super_user" exist?
  • Copy linkTweet thisAlerts:
    @NogDogNov 10.2020 — As far as saving the document, if working on a web host, your PHP script is likely being run via a separate user (nobody, apache, etc.) rather than your personal login accout. Therefore you may need to make sure that user has read/write access on that directory.
    ×

    Success!

    Help @frncskn spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.6,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...