I can't access static files such as js css txt swf, returns a 404 error.

62 views
Skip to first unread message

Simon Cheng

unread,
Sep 19, 2016, 5:43:55 PM9/19/16
to gce-discussion
I was trying to do a php file upload to a folder. I kept receiving a 404 error when I uploaded the files to the folder static.
Here are my files
app.yaml:
runtime: php55
api_version: 1

handlers:
- url: /index.php
  script: index.php

- url: /static
  static_dir: static
php.ini:
 google_app_engine.disable_readonly_filesystem = 1
index.php:
 <?php if(isset($_FILES['UploadFileField'])){ // if($_POST['passwordField']['value'] == "a"){ // }else{ // die("Wrong Password"); // } // Creates the Variables needed to upload the file $UploadName = $_FILES['UploadFileField']['name'];// $UploadName = mt_rand(100000, 999999).$UploadName; $UploadTmp = $_FILES['UploadFileField']['tmp_name']; $UploadType = $_FILES['UploadFileField']['type']; $FileSize = $_FILES['UploadFileField']['size']; // Removes Unwanted Spaces and characters from the files names of the files being uploaded// $UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName); // Upload File Size Limit // if(($FileSize > 125000)){ // die("Error - File too Big"); // } // Checks a File has been Selected and Uploads them into a Directory on your Server if(!$UploadTmp){ die("No File Selected, Please Upload Again"); }else{ move_uploaded_file($UploadTmp, "static/$UploadName"); } }?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><style>body { margin:0; padding:0; background-color:#CCC; }.fileuploadholder { width:400px; height:200px; margin: 60px auto 0px auto; background-color:#FFF; border:1px solid #CCC; padding:6px;}</style><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>File Upload</title></head><body><div class="fileuploadholder"> <form action="/index.php" method="post" enctype="multipart/form-data" name="FileUploadForm" id="FileUploadForm"> <!--<input type="password" name="passwordField" id="passwordField"></input>--> <label for="UploadFileField"></label> <input type="file" name="UploadFileField" id="UploadFileField" /> <input type="submit" name="UploadButton" id="UploadButton" value="Upload" /> </form></div></body></html>
And one empty directory.

Nick

unread,
Sep 20, 2016, 4:31:21 PM9/20/16
to gce-discussion
Hey Simon,

This issue is related to App Engine, not Compute Engine, although it was posted in the Compute Engine group. Also, it appears to be a question requiring more work before it might be posted to Stack Overflow, rather than Google Groups, since Groups is meant for more high level discussion. I'll be happy to assist get the question to that point nonetheless, since some things need clarifying: Could you clarify what you get a 404 for?

Cheers,

Nick
Cloud Platform Community Support

Simon Cheng

unread,
Sep 20, 2016, 7:07:02 PM9/20/16
to gce-discussion
That was when I was accessing an uploaded file. I was even trying to access an already deployed file in the Upload's directory when it returned a 404 error.

Nick

unread,
Sep 21, 2016, 4:10:48 PM9/21/16
to gce-discussion
Hey Simon,

Could you clarify why you believe that you should not have seen a 404 for the file? What is the directory structure of the uploaded project?


Cheers,

Nick
Cloud Platform Community Support

Simon Cheng

unread,
Sep 21, 2016, 7:28:35 PM9/21/16
to gce-discussion
Like this:
Files: app.yaml index.php php.ini
Directories: static (nothing inside)

On
Wednesday, September 21, 2016 at 4:10:48 PM UTC-4, Nick wrote:

Nick

unread,
Sep 22, 2016, 3:33:03 PM9/22/16
to gce-discussion
Hey Simon,

Thanks for that. Now which file were you trying to access? What was the URL path? Because it seems as though, given your directory structure and app.yaml, the only possible url path that will serve a file will be /index.php, where the dot can be any character. This is because, as the docs point out:

URL and file path patterns use POSIX extended regular expression syntax

(source)

I hope this is helpful. Let me know in your reply what it is you tried to access and what you're expecting to see.


Cheers,

Nick
Cloud Platform Community Support 

Nick

unread,
Sep 22, 2016, 3:40:26 PM9/22/16
to gce-discussion
Just to clarify on my own end, part of the reason the only file that will return a match is index.php, is because the static directory is empty. Also, it appears that the code in your original post had its formatting lost in being posted. Here's the properly-formatted code:

<?php
if (isset($_FILES['UploadFileField'])) {
    
    // if($_POST['passwordField']['value'] == "a") { 
    // }else{ 
    // die("Wrong Password"); 
    // } 
    // Creates the Variables needed to upload the file 
    
    // }else{ 
    // die("Wrong Password"); 
    // } 
    // Creates the Variables needed to upload the file 
    $UploadName $_FILES['UploadFileField']['name'];
    
    // $UploadName = mt_rand(100000, 999999).$UploadName; 
    
    $UploadTmp  $_FILES['UploadFileField']['tmp_name'];
    
    $UploadType $_FILES['UploadFileField']['type'];
    
    $FileSize   $_FILES['UploadFileField']['size'];
    
    
// Removes Unwanted Spaces and characters from the files names of the files being uploaded
    // $UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName); 

    // Upload File Size Limit 
    // if(($FileSize > 125000)){ 
    // die("Error - File too Big"); 
    // } 
    // Checks a File has been Selected and Uploads them into a Directory on your Server 
    
    // $UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName); 
    // Upload File Size Limit 
    // if(($FileSize > 125000)){ 
    // die("Error - File too Big"); 
    // } 
    // Checks a File has been Selected and Uploads them into a Directory on your Server 
    if (!$UploadTmp) {
        die(
        die("No File Selected, Please Upload Again");
    } else {
        
    } else {
        move_uploaded_file($UploadTmp"static/$UploadName");
    }
    
}
?>

It appears this code is expecting to be able to write files to the file-system. This will not work in a Standard Environment App Engine app, as explained in the documenation.

Anyways, I hope my explanations have helped. Let me know what you think in your reply.

Sincerely,

Nick
Cloud Platform Community Support

Simon Cheng

unread,
Sep 22, 2016, 4:53:21 PM9/22/16
to gce-discussion
This is using PHP. And I disabled the readonly filesystem as shown in the php.ini file. If anything doesn't work, then what???


On Monday, September 19, 2016 at 5:43:55 PM UTC-4, Simon Cheng wrote:

Nick

unread,
Sep 22, 2016, 5:22:46 PM9/22/16
to gce-discussion
Hey Simon,

Apologies for not noticing that sooner and clarifying this, I believe this is a slight confusion resulting in you looking for behaviour from the platform which isn't there. The docs on this feature say:

  • google_app_engine.disable_readonly_filesystem - By default PHP applications are not allowed to write to the filesystem. Setting this directive to "1" will lift the restriction for the local development environment. However, the setting does not apply to applications running in production, which are always subjected to the sandbox restrictions.
So, this is likely the reason you can't find files in that folder. 

In order to overcome this issue, you could deploy your application as an apache PHP server, so that it could write to disk, or you could have your application use Cloud Storage to store its files, and remain on App Engine.

Let me know if you have any further questions!


Cheers,

Nick
Cloud Platform Community Support


Simon Cheng

unread,
Sep 27, 2016, 9:56:09 AM9/27/16
to gce-discussion
If I use apache (which I wished), you have to use the compute thingy right? (I hope not)


On Monday, September 19, 2016 at 5:43:55 PM UTC-4, Simon Cheng wrote:

Nick

unread,
Sep 27, 2016, 5:40:52 PM9/27/16
to gce-discussion
Hey Simon,

Most generally, Apache servers are deployed in a virtual server environment, such as Compute Engine, yes. But it seems you have a desire to also make use of App Engine PHP features? Let me know more about the details of your app and how it's currently implemented, and I should be able to provide some assistance and guidance in making a choice of how to achieve what you want.


Cheers,

Nick
Cloud Platform Community Support 

Simon Cheng

unread,
Sep 28, 2016, 6:16:04 PM9/28/16
to gce-discussion
Thanks for all your time!!! What I really want is not take money from my pocket and create a "apache" upload and download test server no matter what size (of course not like big 1 GB or small 1 KB, like around 10-50 MB).

Nick

unread,
Sep 29, 2016, 12:31:12 PM9/29/16
to gce-discussion
Hey Simon,

In order to host an apache server, you'll need to use Compute Engine, since there's currently no way to host an apache server in App Engine. You can read about setting up Apache on Compute Engine in the documentation:

* Compute Engine Documentation: Running a basic Apache web server

* Compute Engine Documentation: Setting Up LAMP on Compute Engine

However, App Engine does have a free tier of usage for the default runtimes. What I can recommend is to implement a PHP server that, instead of storing uploaded files in (or fetching files requested for download from) the disk, instead using Cloud Storage. You can find info on how to do this in several docs pages:

* App Engine PHP Documentation: Allowing Users to Upload Files 

* App Engine PHP Documentation: Reading and Writing Files

You can also read about more advanced questions such as permissions, metadata, and caching, in this doc:

* App Engine PHP Documentation: Advanced File Management

I hope this is helpful, let me know if you have any questions about these documents. Be aware that Cloud Storage does have a small charge for stored data (see Cloud Storage Pricing), and that the App Engine free tier of service will begin to throw errors and shut down instances if your usage goes above the daily quota (see App Engine Pricing and App Engine Quotas). 


Cheers,

Nick
Cloud Platform Community Support


Reply all
Reply to author
Forward
0 new messages