Fix Server Timeout Issues in Hotel Directory Database Imports

发布于 2026-07-26 23:18:33

Importing 15,000 Hotels via CLI Without Breaking Your Server


A few weeks ago, I worked with a client who wanted to launch a regional travel booking site. They had a huge list of nearly 15,000 hotels in a single Microsoft Excel CSV file.

Their original developer tried to import this list through a standard browser upload form. Every time they clicked "Submit," the browser spinner would spin for 30 seconds and then display a "504 Gateway Timeout" page. The server simply did not have enough PHP memory to read, process, and write thousands of hotels all at once.

Let me walk you through why this happens and how I used a simple command-line script to bypass the issue.

The Problem with Standard Web Imports

When you import files through a browser, PHP works on a tight timer. This is usually set to 30 or 60 seconds. If your server is busy geocoding hotel addresses, mapping coordinates, and inserting database rows, it will easily hit that limit and stop.

To bypass this, we have to move the work away from the web browser. The command-line interface (CLI) is the best tool for this. It runs with almost no time limits.

Step 1: Writing a Custom CLI Script

Instead of using standard web requests, we can execute our import directly on the server terminal. You can write a basic PHP script that opens the CSV file and reads it line by line.

You can read about how this environment works in the PHP Command Line SAPI documentation. Running scripts this way gives you full control over memory limits and execution times.

Here is a simple example of how I structured the backend command-line loop:

#!/usr/bin/php
<?php
if (php_sapi_name() !== 'cli') {
    die("Only run this from the server command line!\n");
}

$file = fopen('hotels.csv', 'r');
while (($row = fgetcsv($file)) !== FALSE) {
    // Read data line-by-line
    $hotel_name = $row[0];
    $city = $row[1];
    // Insert into database...
    echo "Successfully imported: " . $hotel_name . "\n";
}
fclose($file);

By reading the CSV line-by-line with fgetcsv(), the server only holds one hotel in memory at a time. This keeps our memory footprint extremely low, usually under 10 megabytes.

Step 2: Selecting the Directory System

If you are building a directory site, starting with a clean framework saves you a lot of database design work. For this project, we migrated the client's site to the HotelPoint – Hotel Listing Directory template.

I acquired this listing system from GPLPAL. The database schema in this package is very straightforward, which made it extremely easy to write SQL insert queries for our custom CLI importer. When working on directories or custom booking portals, browsing a reliable PHP Scripts download directory is a good option to find solid base frameworks that you can modify for custom backend tasks.

Step 3: Executing the Safe CLI Import

To run the import, log into your server via SSH. Navigate to your directory folder and run the command directly:

php import_hotels.php

If your CSV is incredibly large, you can run it in the background by appending > import.log 2>&1 & so that you can close your terminal and let it finish overnight.

Keep It Simple

Importing thousands of rows of real estate or hotel directory data does not have to crash your site. Avoid using complex browser uploaders for heavy workloads. Keep it simple with terminal-based scripts, monitor your server resources, and choose a clean database layout to build on.

0 条评论

发布
问题