0

I have a strange situation where I need to send some image data to a php file from within the angular application itself... yes, the php file resides in the angular docroot.

Every time I try to upload image data (from the browser) to this file, the application doesn't seem to go through to it. Instead, I get back a response that is the entire index.html code. I understand that this is probably because I am using ui.router to handle my routing, and have it defaulting to index.html. However, I have tried to add app.use("/upload.php", express.static(path.join(__dirname, '/upload.php'))); to my node server script... in hope that the /upload.php url would be succesfully routed... however, I still only get back index.html code and it seems that the upload.php file is never reached.

Can anybody provide any hinters? Much appreciated.

Grateful
  • 9,685
  • 10
  • 45
  • 77
  • A node server can't run PHP code. If you're trying to execute that `upload.php` file, routing is not the problem, even if you get the route right, `express.static` will simply server that PHP file to the client without executing it. – Titus Apr 23 '17 at 07:56
  • Take a look at the answers to this [question](http://stackoverflow.com/questions/6542169/execute-php-scripts-within-node-js-web-server) maybe you can find something helpful there. – Titus Apr 23 '17 at 07:57
  • @Titus Okay let me add that I have an apache server in place locally at `var/www/`... and I also happen to run my node & angular code in that webroot. – Grateful Apr 23 '17 at 07:58
  • @Titus The php script doesn't have to be within the same application directory.. I just need to be able to pass data from angular to a local php file. How can I do that? – Grateful Apr 23 '17 at 08:00
  • 2
    It doesn't matter what is the public folder of the apache server what matters is on what port it is running. You don't need to go through the node server to get to that php file you can simple reach it directly from the client side eg: `$http.get("domain.com:8080/upload.php").then(....)` where `8080` is the port of the apache server. – Titus Apr 23 '17 at 08:04
  • @Titus That sounds right... But I can't seem to be able to access it in a local setting. My angular app (not apache) is using the port `8080` and is accessed by `http://localhost:8080/ng-upload-admin` and to access the upload script directly I am simply doing `http://localhost/upload.php` (since I don't normally add a port to access apache)... however, I am getting back `No 'Access-Control-Allow-Origin' header is present on the requested resource.` errors in the browser console. – Grateful Apr 23 '17 at 08:15
  • Since you are running under a different port, one for the SPA and the other for the server, you need to enable `cors` requests on the PHP side. – Aluan Haddad Apr 23 '17 at 09:01
  • @AluanHaddad How? – Grateful Apr 23 '17 at 09:11
  • @AluanHaddad Also, it seems to me that the access errors are coming from the angular side.. not php. – Grateful Apr 23 '17 at 09:12
  • That I do not know, because I do have not written PHP for years, sorry. But the principle is the same for any web server. Just lookup how to enable `cors` (Cross Origin) requests in PHP – Aluan Haddad Apr 23 '17 at 09:13
  • 1
    You will need to add a header to allow access, more details here: http://stackoverflow.com/questions/8719276/cors-with-php-headers – Titus Apr 23 '17 at 09:30

0 Answers0